How does npm know the path to install module from - node.js

I am new to npm, using package.json to define the dependencies. I am just curious where is those dependencies installed from?

First, npm checks your local machine for packages installed globally, or using the -g flag.
ie: if you used npm install -g moduleName
You can run npm list -g to figure out where that is on your machine.
Otherwise, npm goes to where you set the npm registry. By default this is npmjs.
If you want to see the default settings, run npm config list

Related

Package visible in npm list, but cannot be required

I am using nvm and via it I have set node v8.11.3 as my default. I installed the varint package globally using npm install -g --verbose varint, specifically to see where it is being installed. Post install on running npm list -g I can see it, but requiring it in the node repl fails. I am not sure as to what the issue is here. I am also not in a directory which has a package.json from before.
UPDATE:
which npm : ~/.nvm/versions/node/v8.11.3/bin/npm
which node : ~/.nvm/versions/node/v8.11.3/bin/node

Using --ignore-scripts for one dependency in NPM

Following this question, NPM dependencies can be installed using:
$ npm install --ignore-scripts
Is there a way to mark that a dependency should be installed without running scripts in package.json?
This is because, when I run npm install --ignore-scripts, the dependency is added to package.json. As a result, other users will install the package while running scripts, however I want this certain package to never run scripts.
I could be wrong but I believe its: npm install -ignore-script package-name#version

"npm config set global false" not working

For some reason my npm is configured to install packages globally. I may have selected that option when I initially installed npm. However, I'm trying to change that configuration so npm installs locally. I googled and found this option:
npm config set global false
However, npm is still installing globally instead of locally. Am I missing a step or doing something incorrectly? Would you expect the command above to do what I need it to do?
npm installs packages locally by default. If you want to install a package globally use flag -g.
e.g. npm install my-package -g

How to tell what’s installed under npm

I just want to get a snapshot of what's installed locally in a project and what's installed globally:
npm whats-installed-locally
npm whats-installed-globally
npm whats-dependencies-between-local-global-repos
What's the proper command-line sequence?
Also, is there a way to determine which dependencies are being unhinged between the two repositories?
To list the local installed npm packages:
npm list
To list the globally installed npm packages:
npm list -g

"npm install" independent of npmjs.org

npm install downloads packages from npmjs.org, compiles and then installs. So even if the node_modules folder is taken backup it can not be used on other machines where the os might be different, due to the native machine code generated during the npm install.
Also another problem with npm install is that it downloads from npmjs.org. What if the site goes down?. How to download the packages and the same package be installed offline on all platforms?
You can tell npm to use a mirror if npmjs.org is down. For example:
npm set registry http://registry.npmjs.eu/
Or with a runtime option:
npm --registry http://registry.npmjs.eu/ install express
If you do keep the node_modules directory with your code, you can simply run npm rebuild to re-compile anything that needs it.
Otherwise, you have a lot of options for installing from various locations. You could keep package tarballs locally. From the documentation:
npm install (with no args in a package dir)
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install [#<scope>/]<name> [--save|--save-dev|--save-optional] [--save-exact]
npm install [#<scope>/]<name>#<tag>
npm install [#<scope>/]<name>#<version>
npm install [#<scope>/]<name>#<version range>
npm i (with any of the previous argument usage)

Resources