Using npm how can I download a package as a zip with all of its dependencies included in the package - node.js

What I'm trying to do is download packages with all their dependencies, in order to transfer them to another computer that does not have an internet connection and install it there.
So the scenario would be:
Download package (to zip/tarball/whatever file) without installing it.
Included in that downloaded file would be all of its dependencies (correct versions, and it's dependencies' dependencies).
Transfer file to other computer.
Run npm install to file location (optional -g important).
Package is installed with dependencies.
Happy camper.
I feel like there has to be a npm command to download and pack (create) files this way.
I've tried looking for a solution for this to no avail.
This is my first time using node so I'm affraid I'm not researching it correctly because lack of knowledge of the node/npm lingo.

I just used this gist by Jack Gill to do exactly what you describe -- bundle up a package, with all its dependencies. Basically what the script does is re-write a module's package.json file to move all its dependencies to bundleDependencies, then pack the whole thing. Upload the resulting tarball to your server, then npm install it. Works a treat.

Download the package to a machine with internet.
Make sure your app package has a package.json file at its root with all of your dependencies listed in it. You can make npm save your dependencies in package.json by doing npm install dependency-name --save. The --save flag will cause npm to write the dependency to your app's package.json file if it has one. If it doesn't have on then it will do nothing. You can also instruct npm to create a package.json file for your app if you need to by simply running npm init from in your app's directory.
Run npm install from inside the app's directory. This will create the node_modules directory and install all the dependencies listed in the app's package.json file.
Zip up the directory now that it has a node_modules directory in it with all your dependencies installed. Transfer the zip archive to another machine.
Simply unpack the archive in its final destination and you're done. The app is now where it needs to be and the dependencies are already installed.
Now just run the application with node app.js, replacing "app.js" with whatever the name of the app's main entry point file is.

You can just use the npm pack command.
So for example:
npm pack lodash
This command will download the npm package and create a file lodash-4.17.4.tgz.
Installing this can be done with:
npm install ../../my-location/lodash-4.17.4.tgz
More details here:
https://docs.npmjs.com/cli/v8/commands/npm-pack

Simply run npm install in the package directory and archive the entirety of it.
Assuming there are no non-npm requirements you need to meet and both machines are running the same version of node, nothing more needs to be done. All of the downloaded dependencies will be installed inside the ./node_modules. But it is a generally good idea to archive the entire package, as the developer might have implemented some additional setup routines.

you can download package with all its dependencies with its dependents using single command. Kindly refer this link npm-package-downloader

Related

Install npm global from a local directory with dependencies

There's a public tool (csso-cli) that can be installed with npm install -g csso-cli to be available globally. Now I need to make a modification to it and change one of its dependencies (csso) to a newer version to use the latest features. So I cloned the repository and installed that with npm install -g ./. It did copy that local code directory to my global npm installation location, but none of the required dependencies were added. There simply isn't a node_modules subdirectory in the install location.
How can I properly install an npm package from a local directory, including all external dependencies, as if it were installed from a public repository? Maybe I need to create some sort of package file first and install that? I don't know much about npm. I searched the web but couldn't find anything. (All instructions are missing the dependencies.) I've read the npm documentation but am still no wiser. That topic isn't covered here, still only incomplete installations without dependencies seem possible. Who needs that?
Looks like I needed the npm pack command. It creates an archive of the package from the local directory. When installing that file with npm install -g my-package-1.0.0.tgz all dependencies are properly installed as well. Plus, the package file doesn't contain the git files.

Using npm to only install the packages needed in current project

I am just starting with node/npm and I have a lot of trouble with
the path to install the package
loading the package in node
I would like to have a package folder (no matter its path) with only the packages needed for my current project (I don't use a package.json just the normal npm install...). So instead of installing the package in the folder given by npm root, I thought I would install all the packages in a local folder with npm install --prefix ./node_modules pck_name.
If I install the packages globally, I am able to load the packages in Node with require('pck-nam'), but when I install in the local folder, I am unable to load the package in Node even by adding the folder path to node_path or with the full path of the packages in require:
const pck = require('C:/Users/Me/myproject/my_modules/node_modules/pck-name');
The error is Cannot find module 'pck-name'
Because I was stuck on this for a long time without finding a solution, I though of renaming the folder given by npm root and then doing a global install : because the folder is will be recreated from scratch, then I will just have the packages for my project. But after the install, I did npm list, and all my previous package are listed, including the one for current project.
I have read many questions/answers and many tuto but I am still unable to use npm/node the way I would like (I am used to python and I regularly use import for global/local modules so I may be thinking too much in a python way).
I can at least partially answer my question as I understand know why the previous package where still there after I renamed the folder. Although I didn't install from a package.json, npm install do create a package.json, or in my case a package-lock.json. And apparently when running a npm install package-name it will check package-lock.json re-install all the missing packages.
So it's not enough to rename the folder indicated by npm root, I also add to rename the package-lock.json. Now I am clear. I still think that I haven't found the best way to go but at least I have what I need.

NodeJS - installing local module

npm i /path/to/module/folder will create a dependency for your project on local module
it creates a link to that folder as the docs says
• npm install < folder >:
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
there is a flag --no-bin-links that prevent creating links only to bin's files
What i want is : is there a way to be able to make dependency on that local module by copy it not link it, so i can make changes and won't reflect on other projects until i manually npm update my-local-module it
PS : from what i searched that was the default behavior of npm install <folder> but they changed it.
You can create a tarball from your-local-module with npm-pack and then install it offline using npm-install:
npm install <tarball file>
Install a package that is sitting on the filesystem. Note: if you just want to link a dev directory into your npm root, you can do this more easily by using npm link. The filename must use .tar, .tar.gz, or .tgz as the extension.

npm link does not care for "files" in package.json or .npmignore

My goal is to specify what files will be included in my node module before publishing it and be able to test the installation locally. Using the "files" in the package.json works in that if I publish my package and install it, then I get only what was specified in "files".
This isn't the case when I use npm link. Be it "files" in package.json or an .npmignore, npm link always seems to give me every file. How can I test my modules installation locally like this?
Ex:
cd ~/projects/node-redis # go into the package directory
npm link # creates global link
cd ~/projects/node-bloggy # go into some other package directory.
npm link redis # link-install the package
If ~/projects/node-redis had "files: [lib]" in its package.json, you would expect only lib to show up in ~/projects/node-bloggy after running "npm link redis", but this is not the case.
Aside:
I love node and npm, but if you look at what is in your node modules, there's so many extraneous files like PNGs used in the readme. Modules are ridiculously huge because of this.
UPDATE:
npm install <path>
seems to respect "files" in package.json according to an answer here and others on stackoverflow. I can't speak for other systems but with npm v 6.9.0 on Fedora Linux, this doesn't work as all files are still copied.
Example:
If you need a published module to test this scenario with, I recently published num2cheque which has no dependencies. You will see that if you install it from the npm registry with
npm install num2cheque
you do not receive the test directory which I have locally because in the package.json I specify
"files": [lib]
Add a test directory to your local install then try to use npm link or npm install with a path and you will see that test directory is now included. Hope that illustrates the issue.
Workaround: npm install a GIT repo URL
You may want to install a package from a GIT repo, eg
npm install https://github.com/AndreasPizsa/parse-decimal-number.git
This is an actual npm install which respects the files entry, even if the package has not yet been published to an npm repository.
Background
npm link does not copy, it creates a link
npm link does not actually install the package by copying it into the target folder.
Instead it creates a symbolic link to the source folder, which is why you’re seeing all the files that are in the source folder ("node-redis"), not just those specified in files.
This behavior is documented in the npm link documentation:
First, npm link in a package folder will create a symlink in the
global folder {prefix}/lib/node_modules/ that links to the
package where the npm link command was executed. (see npm-config for
the value of prefix). It will also link any bins in the package to
{prefix}/bin/{name}.
Next, in some other location, npm link package-name will create a
symbolic link from globally-installed package-name to node_modules/ of
the current folder.
https://docs.npmjs.com/cli/link.html
"What’s a Symlink?" you may ask:
a symbolic link (also symlink or soft link) is a term for any file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution.
https://en.wikipedia.org/wiki/Symbolic_link
If your concern is the use of space on your hard disk, worry not - nothing is copied or duplicated, only linked (just like linking to Wikipedia doesn’t duplicate it, it actually saves space)
... and so does running npm install locally
npm install with the path to the package will also create a symbolic link to the package in question. A helpful scenario for this can be a module that’s still under development.
cd ~/projects/node-bloggy
npm install ~/projects/node-redis
This will create a symbolic link under node_modules in your node-bloggy project.

npm link, without linking devDependencies

It appears that when I run npm link, it will install the project globally, and it seems to install devDependencies with it.
Is there a way to run npm link without devDependencies, perhaps with the --only=production flag?
In npm#4.x or lower
When you run npm link in other_module then you will get both dependencies and devDependencies symlinked.
The --production flag doesn't change anything, still creates a symlink to the whole directory
In npm#5.1.0
They fixed it!
If you remove node_modules and then do npm link --only=production, it runs an install before symlinking, and therefore devDependencies folder are indeed excluded.
This is currently not possible with npm link. The problem is, if you install only prod dependencies in that dependency, you're able to link it, but you're not able to develop on that dependency anymore (since missing devDependencies). And vice-versa: If you install devDependencies, you can't link anymore.
The solution: A package called npm-local-development at https://github.com/marcj/npm-local-development
It basically does the same thing as npm link, but works around the devDependency limitation by setting up a file watcher and syncs file changes automatically in the background, excluding all devDependencies/peerDependencies.
You install npm-local-development: npm i -g npm-local-development
You create file called .links.json in your root package.
You write every package name with its local relative folder path into it like so
{
"#shared/core": "../../my-library-repo/packages/core"
}
Open a console and run npm-local-development in that root package. Let it run in the background.
Disclaimer: I'm the author of this free open-source project.
A workaround I use is npm pack then point to the packed file in the example

Resources