If you need to install an npm package for nodejs from local files, because you can't or prefer not to download everything from the npmjs.org repo, or you don't even have a network connection, then you can't just get an npm package tarball and do `npm install <tarball>`, because it will immediately try to download all it's dependencies from the repo.
There are some existing tools and resources you can try:
- npmbox - https://github.com/arei/npmbox
- https://github.com/mikefrey/node-pac
- bundle.js gist - https://gist.github.com/jackgill/7687308
- relevant npm issue - https://github.com/npm/npm/issues/4210
I found all of these a bit over-wrought for my taste. So if you prefer a simple DIY approach, you can simply edit the module's package.json file, and copy all of its dependencies over to the "bundledDependencies" array, and then run npm pack to build a new tarball that includes all the dependencies bundled inside.
Using `forever` as an example:
- make a directory and run `npm init; npm install forever` inside of it
- cd into the node_modules/forever directory
- edit the package.json file
- look for the dependencies property
- add a bundledDependencies property that's an array
- copy the names of all the dependency modules into the bundledDependencies array
- save the package.json file
- now run `npm pack`. It will produce a forever-<version>.tgz file that has all it's dependencies bundled in.
- In online environment,
npm install --no-bin-link
. You will have a entire flattenednode_modules
- Then, bundle this flawless
node_modules
withtar / zip / rar / 7z
etc - In offline environment, extract the bundle, that's it
1 comment:
When using NPM 3 and above, and extra step is required. NPM 3 flattens out the node_modules folder and does not create nested node_modules, so the `npm pack` command results in a tgz without bundled dependencies.
The extra step:
- Run npm install after editing the package .json. This will re-install the node_modules in the folder that will b packed.
Post a Comment