Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to npm install to a specified directory?

Tags:

node.js

npm

Is it possible to specify a target directory when running npm install <package>?

like image 212
coudy Avatar asked Jan 22 '13 22:01

coudy


People also ask

What directory does npm install?

npm install (in a package directory, no arguments): Install the dependencies to the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.

Does npm install add to path?

node. js - npm global install does not add packages to PATH on Windows 8.1 - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


2 Answers

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.)

like image 57
coudy Avatar answered Sep 22 '22 15:09

coudy


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.

like image 41
Rohit Sharma Avatar answered Sep 24 '22 15:09

Rohit Sharma