How to set the default path for node modules? - node.js

I have installed node.js at my local system (path : C:\Program Files\nodejs). I installed some modules and expected them to be placed in (C:\Program Files\nodejs\node_modules). But, the installed modules are placed at C:\Users\Administrator\AppData\Roaming\npm\node_modules. Is this right or wrong? Can i access these modules globally?
My system platform is windows 7, and my version of node is 0.10.

The current (January 2018) version of Node.js is 9.4.0, so I'm not sure if it is compatible with your version.
You can set the default global installation path for node_modules by modifying your npmrc file.
Execute in a prompt: npm config list. It should among other things display a prefix setting, which is set to your Roaming AppData folder, for example: C:\Users\Administrator\AppData\Roaming\npm. You can override this setting by executing npm config set prefix C:\Program Files\nodejs\node_modules\npm.
Now, once you install node_modules globally they will be placed in that directory.

Running just npm install will install all modules into a folder in the current directory called node_modules; all files within the same root directory (even in sibling folders), will check for modules here when you call require. You should install any modules that you wish to require in your project this way.
If you want to install a module from npm globally, you can append the -g flag. This is usually for command-line tools, which you want to be accessible across multiple projects. An example would be npm install nodemon -g
If you are still confused, I recommend you reference this blog post from the makers of node on global/local installation.

You can find out default paths (user's and global's ones) by command:
npm config list
It is in 'prefix' variable, e.g.:
; userconfig C:\Users\pavel\.npmrc
cache = "C:\\ProgramData\\npm-cache"
prefix = "C:\\ProgramData\\npm"
To change default path have to use the command, e.g.:
npm config set prefix="C:\ProgramData\npm"

By default, any packages you install will install to a global installation directory which is why they are showing up in C:\Users\Administrator\AppData\Roaming\npm\node_modules. If you want to install the packages to your local node_modules folder you will need to type in the following:
npm install (package name) --save-dev

Related

How to relocate global packages?

In the process of getting familiar with npm, ive installed a bunch of stuff globally, including angular cli - into my users directory on windows (C:\users...)
How can I either relocate this directory or uninstall and reinstall to a different folder?
You could do it one by one by
npm uninstall angular-cli
npm -g install angular-cli
without -g will interact with current directory package.json while with -g do it globally (your %USER_PROFILE%)
I don't think you can change directories like that. Global installation chooses the path to {prefix}/lib/node_modules by default as the program refers to this path when looking for -g modules. You can't change them.
For local installation files automatically go to ./node_modules/.bin/ and program checks from there.
Please see https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/

Access globally installed package via require in node

I have a gulp based build system. Currently I have some tasks, e.g. a task that is accessing npm (require('npm')) programmatically. In order to achieve it I need to specify npm in my package.json dependencies, so that require can find it inside node_modules. However npm is obviously available along with node & I also have npm installed globally (latest 3.x version of npm).
Is there a way to require a global instance of npm? I do the same stuff e.g. with other npm packages (I mean I have global package installed but I duplicate it in package.json to make it available via require).
Yes you can do it by adding NODE_PATH to your environment variable eg:
export NODE_PATH=/usr/local/lib/node_modules/
After doing this you node should be able to find globally installed packages as well.
NOTE:your node module path may defer.
to make it permanent you can add the commmand to your ~/.bashrc file

Not able to request global modules

I have installed two modules globally which are discord.js and request. When I do npm list -g, I seem them in there. When I go to my project folder and fire up a command prompt, I type node app.js and it says it cannot find discord.js, why is this? I had just reinstalled Windows and this was working perfectly before the reinstall.
When you run npm list -g you are getting the list of globally installed modules instead of those in the currently installed project.
Globally installed modules reside in something like /usr/local/lib/node_modules, whereas locally installed modules reside in /your_project/node_modules.
From the npm documentation for npm ls -g (aliased in your case to npm list -g):
global
Default: false
Type: Boolean
List packages in the global install prefix instead of in the current project.
The Node.js require does not look in the global node_modules folder, only your local node_modules folder.
You can either add your global node_modules folder to your NODE_PATH environment variable or install using npm install without the -g flag. When you run npm install --save <module name> you install the module in the node_modules folder for the current project, which allows your project to run properly.
Apparently I had to do npm install --save <module name>
Even though it's the answer to my problem, can anybody answer why it did this?

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.

npm package.json install globally?

Is there any way to set an NPM dependency to be installed globally in the package.json file?
Whenever I run npm update all the dependencies are installed locally.
Thanks.
I believe that the -g option causes things to be installed globally.
Is your reason for installing globally in order to make new scripts available on the command line? If so, I might have a workaround for you.
Just install your packages as usual (without the -g):
npm install -S my_module_name
Including the -S flag or --save will help keep your package.json file up to date.
As usual, your project's npm install step will install locally (as you have described). However, it will also produces a local folder containing symlinks to each of the project's npm-supplied command-line executables (located inside the node_modules/.bin/ folder).
Add that folder to your system path to enable command-line access to npm modules without requiring installation via -g, or root access to a machine:
export PATH=/path/to/your/project/source/node_modules/.bin/:$PATH

Resources