329

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

coudy
  • 13,218
  • 5
  • 23
  • 26
  • Also see: http://stackoverflow.com/questions/14742553/npm-local-install-package-to-custom-location – Rimian Sep 15 '15 at 01:14

5 Answers5

438

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

Molomby
  • 5,859
  • 2
  • 34
  • 27
coudy
  • 13,218
  • 5
  • 23
  • 26
  • 83
    It appears you have to have the package.json file in this alternate directory if you use the --prefix option. Is there a way to have package.json in the current directory and install to the alternate directory ? –  Jun 04 '15 at 19:30
  • 2
    @fractal See my answer for that – yunzen Oct 01 '15 at 08:57
  • 10
    @coundy Is there a way to specify the path for `npm install` (without package name)? I thired this directly, but then npm looks for `package.json` in the `{prefix}`. What I also aim is to be able to let the `packages.json` in the project root and in the same time install all packages in `{project root}/{project webroot}`. – automatix Feb 22 '16 at 10:38
  • 1
    I ran into a recursion issue. Is there a way to tell it to run once? I solved it temporarily by changing directory and then calling `npm install`, but `--prefix` seems like a nicer solution, if it wasn't causing a recursion. – adi518 Aug 10 '18 at 17:55
  • this does not work, it will just create a symlink, everything is still install to your source dir – Wang Sep 04 '20 at 20:33
  • This is not reading the package.json file. It is just installing test module for some reason. – Andy N Aug 26 '21 at 21:49
  • 1
    Note that this feature is [not documented and might be removed in a future version](https://github.com/npm/cli/issues/1368#issuecomment-1241076700). – Yogu Sep 08 '22 at 18:34
79

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.

Community
  • 1
  • 1
Rohit Sharma
  • 1,844
  • 1
  • 11
  • 10
  • 2
    This doesn't seem to work , I always get ``Can't install : Missing package name`` – Tibor Blenessy Jan 24 '18 at 12:07
  • 1
    @TiborBlenessy Assuming that you're talking about the second installation statement. Please make sure that you've a valid package.json file in your current directory. If it still doesn't work then, do let me know your npm version. These steps used to work with the specified npm version. And, if they don't anymore, I would love to dive deep and get to the root cause and may be I can come up with an alternative. – Rohit Sharma Jan 30 '18 at 10:24
  • 4
    @RohitSharma Using npm 6.0.0 this method does not work as it expects the `package.json` file in `./install/here` – Christian Ivicevic Apr 30 '18 at 10:16
  • @ChristianIvicevic 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 with npm 6.0.0 – Rohit Sharma May 03 '18 at 08:04
  • 1
    This just plain doesn't work. It expects the json file to be in the ./install/here directory even when you put ./ at the end. I'm also not impressed that it creates that unnecessary symlink to the parent folder. Is it really this hard to tell NPM where the installation path should be and it still can't do it smoothly? – Tanoro Aug 07 '19 at 14:41
  • @Tanoro Are you sure that the above mentioned commands do not work with npm 3.8.6 and 6.0.0? Which npm version are you using? They used to work when I added the answer. – Rohit Sharma Aug 08 '19 at 11:43
  • @Rohit Sharma I tried this on v3.5.2 and then updated to 6.0.0. Your second command above throws an error in both versions suggesting NPM is looking inside the prefix directory for package.json. If I try your 3rd command, that does install the dependencies, but it also unnecessarily symlinks the parent directory inside the prefix directory, which is problematic for me. – Tanoro Aug 08 '19 at 16:14
  • 1
    @Tanoro The third command will work with npm 6.0.0 with symlink creation as mentioned in the answer. I have never tried first 2 commands with npm v3.5.2 which is why I have mentioned the specific version of npm I had tried with. Please feel free to update the answer with working commands for other versions too. – Rohit Sharma Aug 09 '19 at 07:00
  • 1
    in 6.14.4 `--prefix /opt/somedir ./` does not work. It just create a symlink in /opt/somedir/node_modules – Wang Sep 04 '20 at 20:24
57

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

<path/to/prefix_folder>/lib/node_modules
yunzen
  • 32,854
  • 11
  • 73
  • 106
1

There currently is no documented way to install a package in an arbitrary directory. You should change into the target directory, make sure it has a package.json, and then use the regular commands.

While there currently is an undocumented option called --prefix, this feature might be removed in a future release. At least, it it is not planned to ever document this as an official feature.

Yogu
  • 9,165
  • 5
  • 37
  • 58
0

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
Malachy
  • 307
  • 8
  • 16