Npm multiple entry points

I am making an NPM package, and I wonder how you can register multiple entry points so that the user can select either the entire library or only the part that they intend to use.

For example, to enter the entire library:

const mainLib = require('main-lib');

Or just part of it:

const subLib1 = require('sub-lib-1');
const subLib2 = require('sub-lib-2');

It seemed to me intuitive to have the main property of package.json accept multiple values, but this does not look like documentation.

+4
source share
1 answer

"main" defines the module to load when you call require () with only the package name. But you can also require a specific file in this package.

for example with the following package:

- mypackage/
   - main.js   <- "main" in pkg.json
   - moduleA.js
   - src/
     - index.js
     - filaA.js
     - fileB.js
   - package.json

The following are valid:

require( 'mypackage' )           // resolve to main.js
require( 'mypackage/moduleA' )   // resolve to moduleA.js
require( 'mypackage/src' )       // resolve to src/index.js
require( 'mypackage/src/fileA' ) // resolve to src/fileA.js
+11
source

Source: https://habr.com/ru/post/1657824/


All Articles