I created an NPM package that uses Webpack and Babel for transpiling/bundling.
In my package.json
, I've got main set to "main": "build/index.js"
. And in my Webpack config, I have entry set to entry: { app: './src/index.js' }
. My entry file is shown below.
Everything works fine when the package is installed. However, with this setup, two import paths are exposed for every helper:
This is a problem for editors that support auto imports, since they will sometimes auto import from 'my-package/build/utils/helper1'
rather than the preferred path of 'my-package'
.
So, two questions:
Entry File:
import helper1 from './utils/helper1';
import helper2 from './utils/helper2';
export {
helper1,
helper2,
};
const myPackage = {
helper1,
helper2,
};
export default myPackage;
Here are some of the best practises that we can follow while choosing a NPM package — Checking the license type of the package is the most important thing. It gives you the details about terms and conditions on the package use.
Use Packito to clean your package before publishing it I created this tool to go further in packaging npm module. It is a superset of previous step 3. In a dist folder, it will copy mandatory and selected files, but also refactor package.json to remove/change some fields.
The most common way of doing that is by using npm install request. If you’d like to take that one step forward and automatically add it to your package.json file, you can do: npm will save your dependencies with the ^ prefix by default. It means that during the next npm install the latest module without a major version bump will be installed.
You can run npm link package-name from another location, to create a symbolic link from the globally installed package-name to the /node_modules directory of the current folder. Let’s see it in action! The next article in the Node.js at Scale series will be a SemVer deep dive with how to publish Node.js modules.
export {
helper1,
helper2,
};
Recently, I found that we can use Object.freeze()
to export. This is a good article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With