Bundling NPM module for OFFLINE distribution (with all dependencies) - node.js

How can I create a tarball package for distribution (with all its dependencies)?
The package needs to contain the actual module + all its dependencies since it will be installed locally/offline due to internet restrictions on the organization.
I tried adding the dependencies to bundledDependencies in package.json then running npm pack. But the generated tarball does not include any dependencies I have listed.
I also tried using a module called npm-pack-all but it does not work as I intended.
Any way I can do this? Preferably without the need for additional npm modules.
Help is really appreciated. Thanks!

npm pack with bundledDependencies worked well for me:
I am using node v10.15.0 and npm v6.4.1
It even bundled the dependencies that my dependencies need/have.

Related

How to build node.js projects against local versions of the dependencies?

I've been trying to build a relatively complex node.js project (https://github.com/edrlab/thorium-reader/) against local versions of some dependencies.
I can build and run the project with the non-local dependencies without problems.
I've tried different "routes", such as adding the dependencies using npm install --save <path-to-dependency> or just adding a file:<path-to-dependency> reference to the package.json file. I've checked out the exact versions of each dependency.
npm install doesn't show any errors.
Now when I run npm run start I get type errors that don't quite understand, such as:
Argument of type 'import("~/repositories/thirdparty/thorium/r2-opds-js/dist/es6-es2015/src/opds/opds2/opds2-facet").OPDSFacet'
is not assignable to parameter of type
'import("~/repositories/thirdparty/thorium/thorium-reader/node_modules/r2-opds-js/dist/es6-es2015/src/opds/opds2/opds2-facet").OPDSFacet'.
Types of property 'Links' are incompatible.
Does anyone have a hint for me what I am doing wrong here ?
I'm using node.js version 17.2.0 and npm version 8.2.0.
Best,
N
Ok, after fiddling around for hours I found that using npm link does the trick. Not sure what the differences are, npm install didn't work at all, neither did putting the references in package.json.
That is, go to the dependency's repository, run npm link, build the dependency as a module, THEN go to the main repository and run npm link <dependency> --save.
Now the build process works.
Not sure why there are the two different method, one of which doesn't work a all in this case.

How to create npm/yarn dependency tree from just package.json; without creating node_modules folder

I inherited an application which works fine in node8, but npm install fails in node10, giving an error about fibers package being built using node-gyp
fibers is not a direct dependency of the app, so I want to know which dependency is bringing in fibers as it's dependency.
Unfortunately, npm ls, yarn why only works when node_modules is generated completely through npm install or yarn install.
I did research online but couldn't find a static dependency tree generator just from package.json.
Even though I could just use node8 and run npm install followed by npm ls to figure out whose bringing in fibers; I believe there should be an easier static analysis of package.json.
Is there no way to statically analyze a package.json and create a dependency graph for it in npm/nodejs ?
I come from java and we had maven which can just analyze a file named pom.xml to create a nice graph about whats coming from where.
Execute npm install in the directory and let it fail.
It'll output something like
A log of this can be found at <location>
Open the log file and search for the text saveTree.
Notice a hierarchy of resolved packages
Here you can find the module you're looking for and whose bringing it in.

NPM install bunch of packages not from package.json file

Using Visual Studio code as IDE but lately when I run the command - npm install from the app folder of the solution it installs around 374 items under "node_modules" instead of just installing the packages from the package.json file.
Can someone please provide some pointers for this behavior?
My versions:
node -v
v6.9.1
npm -v
3.10.8
Go to your node_modules folder and find one of the folders matching the libraries from your package.json file. Inside you will find another package.json which describes this library. It is most likely it will also have at least a couple of entries in dependencies section.
When you run npm install npm builds so-called 'dependency tree'. It starts with your top-level package.json and checks what dependencies needs to be installed, then (using its registry) it checks what are the dependencies of these dependencies and then their dependencies and so on...
It is prudent (but often neglected) to check what are the dependencies of the libraries you decide to use. Some of them might have licenses incompatible with yours. Some of them might need a ton of code to perform a simple thing. Many will use deprecated versions, which will spam your npm install log with warnings and might actually cause some conflicts with your other dependencies.

npm install generate more folders than needed

I've been using ember for a while and when I wanted to install the node dependencies of a project, I just needed to use npm install to create the folder node_modules with all the dependencies (as it's described in http://ember-cli.com/user-guide/).
Since I was using an old version of node I unisntalled node and npm and installed nvm with the versions node v5.0.0 and npm v3.3.6but now, when I try to use npm install to install the dependencies of a project as I used to do before, instead of the dependencies of the package.json file, I get many, many more from things I'm not sure where they come (I think they are dependencies that npm handles by itself in a globally way but now it's adding them to my project locally, but I'm not sure).
Why am I getting all those unknown (for me) dependencies?
Notice that, when I run ember new it generates the correct dependencies in node_modules but if I delete this folder and run npm install happens the same.
That's one of the changes introduced by npm v3.0:
Your dependencies will now be installed flat - by default. If
possible, all of your dependencies, and their dependencies, and their
dependencies will be installed in your project's node_modules folder
without nesting. Nesting will only occur when two or more modules have
conflicting dependencies.
Read more at http://www.felixrieseberg.com/npm-v3-is-out-and-its-a-really-big-deal-for-windows/

How to prevent npm install <package> --save-dev from reordering devDependencies

Background
We're having issues with a Windows build system hitting the file path too long error when the node modules folder has items within it that have paths which are over 260 characters.
We've discovered adding a deeply nested dependency to the top of the devDependencies section fixes this issue. The assumption is that when npm sees a nested dependency C.1 require package A, which is already declared and available in devDependencies, npm will not add dependency A to dependency C.1's node_modules directory.
Issue
The problem I'm seeing on my local machine is that running npm install <package> --save-dev reorders the packages in devDependencies alphabetically, but the order npm process packages and their dependencies matters. If I check this in, then the build system will hit the same file path too long error.
ie If package A comes after package C and dependency C.1 requires package A, then npm will add package A to the node_modules folder of dependency C.1.
I'm not sure if this reordering is only on my machine since I haven't seen npm reorder dependencies on my home machine before.
Has anyone seen this before or know how to stop this behavior?
Versions
Node: v0.10.32
NPM: v1.4.28
Side note: I've read that npm 2.0 or future versions will analyze the dependency hierarchy, find duplicated packages, and only reference them once on the file system, but the upgrade to npm 2.0 is not in the picture at this time.
The only way I see this working is to have some sort of preinstall script which [hopefully] will run after the dependencies file has been updated but before the package is installed. From the npm site:
In the current version of node, the standard way to do this is using a
.gyp file. If you have a file with a .gyp extension in the root of
your package, then npm will run the appropriate node-gyp commands
automatically at install time
If that doesn't work, you will need to use MakeFile and rewrite the package.json file. This is not too out of the ordinary as some projects require some sort of pre-compilation - you would just instruct your team to run a separate command for installing npm packages.

Resources