Necessary changes required to use import {general_js} from 'general-js-toolkit' instead of import {checkIsEmpty} from 'general-js-toolkit/dist/util'

huangapple go评论71阅读模式
英文:

Necessary changes required to use import {general_js} from 'general-js-toolkit' instead of import {checkIsEmpty} from 'general-js-toolkit/dist/util'

问题

You can modify your published npm package to allow users to import your package using the syntax import { general_js } from 'general-js-toolkit' by specifying the main field in your package.json file. Update your package.json like this:

{
  "main": "path/to/general-js-toolkit.js",
  // ...
}

Replace "path/to/general-js-toolkit.js" with the actual path to your package's main JavaScript file.

This way, users can import your package as follows:

import { general_js } from 'general-js-toolkit';

Make sure to publish the updated package to npm after making this change.

英文:

How can I modify my published npm package to allow users to import my package using the syntax import { general_js } from 'general-js-toolkit', instead of the current syntax import { checkIsEmpty } from 'general-js-toolkit/dist/util'? What changes do I need to make to my package to achieve this?

this is my repo url :

https://github.com/vitabletech/general-js-toolkit 

this is How I want to use
Usage
To use general-js-toolkit in my project, simply include it in our HTML or import it in our JavaScript file:

<script src="path/to/general-js-toolkit.js"></script>
import general-js-toolkit from 'general-js-toolkit';

Once I have included general-js-toolkit, I can use its functions by calling them directly:

const result = general-js-toolkit.someFunction(args);

can someone help me ?

I have tried multiple ways to import the required functions in js file but it always gives an error Object(...) is not a function. any other way of importing could help me out.

答案1

得分: 1

I have seen your code on GitHub. My suggestion is, instead of using Babel with "build": "babel src -d dist", you can use webpack.

You can follow these steps in your project:

  • Install webpack and webpack-cli as dev dependencies with npm install webpack webpack-cli --save-dev.
  • Create a new file called webpack.config.js in your package's root directory with the provided contents.
  • Replace "main": "dist/index.js" with "main": "dist/general-js-toolkit.js" in your package.json file.
  • Add "module": "src/index.js" to your package.json file.
  • Replace "build": "babel src -d dist" with npx webpack --config webpack.config.js.
  • To check the package locally before publishing, run npm pack command.
  • After obtaining the package file, copy it to your project (e.g., your React project).
  • Run npm install <path/of/pack/file>.
  • Import functions using import { function_name } from 'general-js-toolkit';.
  • Use the imported functions, e.g., console.log(function_name(''));.

Here's how both HTML or PHP developers and React developers can use your package, as indicated in your provided code.

英文:

I have seen you code on github, My suggestion is Instead of using of babel &quot;build&quot;: &quot;babel src -d dist&quot; you can use webpack.

you can follow below step in your project

  • npm install webpack webpack-cli --save-dev
  • Create a new file called webpack.config.js in the root directory of your package, with the following contents:
const path = require(&#39;path&#39;);
module.exports = {
  mode: &#39;production&#39;,
  entry: &#39;./src/index.js&#39;,
  output: {
    filename: &#39;general-js-toolkit.js&#39;,
    path: path.resolve(__dirname, &#39;dist&#39;),
    library: &#39;generalJs&#39;,//give the better name if you do not like but if it is set once then we will not change it in the future.
    libraryTarget: &#39;umd&#39;
  }
};
  • replace "main": "dist/index.js", to this "main": "dist/general-js-toolkit.js" in package.json file

  • also add this in package.json file &quot;module&quot;: &quot;src/index.js&quot;

  • replace "build": "babel src -d dist" with this npx webpack --config webpack.config.js

  • To check package in local before publish run npm pack cmd

  • after you got the package file copy it into your any project like your react project

  • run cmd npm install &lt;path/of/pack/file&gt;

  • import { function_name } from &#39;general-js-toolkit&#39;;

  • console.log(function_name(&#39;&#39;));

How General users like HTML or PHP developer use our package

&lt;body&gt;
&lt;script src=&quot;dist/general-js-toolkit.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
        console.log(generalJs.function_name(&#39;&#39;));
      &lt;/script&gt;
&lt;/body&gt;

How react developer can use it

import { function_name } from &#39;general-js-toolkit&#39;;
console.log(function_name(&#39;&#39;));

huangapple
  • 本文由 发表于 2023年5月10日 12:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定