Install npm global from a local directory with dependencies - node.js

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.

Related

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.

Npm Install straight from package.json

I have a simple question that i cant seem to find the answer for. I cloned a git repo to my local machine.
When attempting to start node, I receive an error because i don't have the required npm dependencies installed. However, they are located in the packages.json file that was cloned.
I was wondering if there was a simple way to install the dependencies located in that file, without having to npm install for every individual package.
Within the directory of the package.json file, just run npm install. It will read package.json and install all dependencies. If you want to limit it to only non-dev dependencies, use npm install --only=production.

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.

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

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

Difference between ~/.npm, $PROJECT/node_modules, and /usr/lib/node_modules?

I installed npm and when I did my first sudo npm install some-package -g it installed that package to /usr/lib/node_modules as I expected, but then it also created several files in ~/.npm. What's the difference between these locations?
Other answers here have said that a global installation using -g should install it to your home directory by default, but for me it installs it to /usr/lib/node_modules, am I doing something wrong?
And when I do a local installation without -g it installs to the current directory $PROJECT/node_modules. What's the difference between all these locations, and what should go where?
The system wide package install directory, typically under /usr/lib is usually used for globally installed packages that provide a binary which should be available in your PATH (to be able to execute it from anywhere).
The local install directory node_modules, created by npm install at the location where you're executing npm, is typically located in your project directory and usually used for project specific dependencies.
The ~/.npm contains packages already downloaded. When installing the same package in another location, npm will lookup that package in that cache directory first.
Reference: https://docs.npmjs.com/files/folders
Related files:
.npmrc - NPM configurations at different locations
package.json - Packages and their versions for a project
Hypothetical scenario: two projects using Grunt (a Javascript based buildscripting tool):
Both projects use different Grunt versions. One project is older. Grunt can't be updated without having to adapt the whole build process, another project has just started.
You have to install "grunt-cli" system wide (using the -g flag) since it provides the grunt binary. This CLI binary will lookup for a local "grunt" in your current project directory. The "grunt" npm on the other hand, installed locally (without -g) will then be bootstrapped by the CLI. When downloading grunt for the first project npm will store downloaded packages in ~/.npm, when installing grunt for the second project, npm will lookup packages common to both projects in ~/.npm first.
There are other reasons to install packages globally, the most time they provide a binary that should be located in your PATH.
Alternatively, some packages that typically need to be installed globally can also be installed locally. You'll then have to add the path to that binary (e.g. path/to/your/node_modules/.bin/<BINARY>) to your PATH variable or just specify the full execution path.

Resources