So I am adopting a modular JS approach in my next project, all of which is a brand new process to me. It would be appreciated to keep things rather dumbed down, as my current experience is limited.
So I have my private package up on npm:
@name/package-name
Now, my private package consists of several JS files with exports, to keep things modular and clean during dev time.
I consume this package into my new project:
npm i @name/package-name -S
I require my package from my node_modules, using a loader/bundler such as webpack, requireJS, or the like:
var test = require('@name/package-name');
...
//use the imported code
So, this causes the require to enter my package at its defined Entry point.
Now this is where I am confused. Should the installed private package have an 'entry.js' file which has been previously bundled(from the separate JS files) so that single entry file has everything the code in my new project needs to run?
If so does this mean I run webpack within every module itself to bundle it, before I use webpack to bundle up my new project?
Advice is appreciated, thank you.
Should the installed private package have an 'entry.js' file which has been previously bundled(from the separate JS files) so that single entry file has everything the code in my new project needs to run?
You don't need to bundle it, especially for Node.js server-side code. Let's say your module has the following file structure:
node_modules/@name/package-name/index.js
node_modules/@name/package-name/foo.js
node_modules/@name/package-name/bar.js
and index.js
contains this:
var foo = require('./foo.js');
var bar = require('./bar.js');
When you run require('@name/package-name')
from your project, node will run index.js
and require()
the foo.js
and bar.js
files by loading them from the file system as the code executes.
This dynaimc require-chain is one of the reasons Node.js entrypoints are slow: