node.js - How to npm install to a specified directory?

ID : 10232

viewed : 17

Tags : node.jsnpmnode.js

Top 5 Answer for node.js - How to npm install to a specified directory?

vote vote


You can use the --prefix option:

mkdir -p ./install/here/node_modules npm install --prefix ./install/here <package> 

The package(s) will then be installed in ./install/here/node_modules. The mkdir is needed since npm might otherwise choose an already existing node_modules directory higher up in the hierarchy. (See npm documentation on folders.)

vote vote


As of npm version 3.8.6, you can use

npm install --prefix ./install/here <package> 

to install in the specified directory. NPM automatically creates node_modules folder even when a node_modules directory already exists in the higher up hierarchy. You can also have a package.json in the current directory and then install it in the specified directory using --prefix option:

npm install --prefix ./install/here 

As of npm 6.0.0, you can use

npm install --prefix ./install/here ./ 

to install the package.json in current directory to "./install/here" directory. There is one thing that I have noticed on Mac that it creates a symlink to parent folder inside the node_modules directory. But, it still works.

NOTE: NPM honours the path that you've specified through the --prefix option. It resolves as per npm documentation on folders, only when npm install is used without the --prefix option.

vote vote


In the documentation it's stated: Use the prefix option together with the global option:

The prefix config defaults to the location where node is installed. On most systems, this is /usr/local. On windows, this is the exact location of the node.exe binary. On Unix systems, it's one level up, since node is typically installed at {prefix}/bin/node rather than {prefix}/node.exe.

When the global flag is set, npm installs things into this prefix. When it is not set, it uses the root of the current package, or the current working directory if not in a package already.

(Emphasis by them)

So in your root directory you could install with

npm install --prefix <path/to/prefix_folder> -g 

and it will install the node_modules folder into the folder

vote vote


I am using a powershell build and couldn't get npm to run without changing the current directory.

Ended up using the start command and just specifying the working directory:

start "npm" -ArgumentList "install --warn" -wo $buildFolder 
vote vote


Top 3 video Explaining node.js - How to npm install to a specified directory?