185

I have the following directory structure:

/some_project
    source.js
    package.json

I would like to install the dependencies for some_project. I know I could cd into some_project and then run npm install

But I was wondering if it's possible without changing the directory ? Something like

npm install some_project/package.json 
Florin
  • 2,891
  • 4
  • 19
  • 26

5 Answers5

431

You can use the npm install <folder> variant with the --prefix option. In your scenario the folder and prefix will be the same:

npm --prefix ./some_project install ./some_project
coudy
  • 13,218
  • 5
  • 23
  • 26
  • 3
    This should really be marked as the correct answer, though I don't think the last `./some_project` argument is required – itsananderson Jun 23 '15 at 20:26
  • 6
    BTW, it creates empty `etc` folder in destination, it's a known bug https://github.com/npm/npm/pull/7249 – Michael Radionov Oct 28 '15 at 09:03
  • 8
    Thanks for this answer. Is there any more clarity on whether the second `./some_project` is required? – Aron Feb 23 '17 at 10:02
  • 22
    the second `./some_project` is not required – JRJurman Sep 14 '17 at 06:11
  • Is it also possible to omit the `node_modules` folder completely in such a case? i.e. let's say I want to have these packages all in a folder called `/vendor` (and not `/vendor/node_modules`). – fritzmg Dec 08 '17 at 08:31
  • 3
    The second `some_project` is definitely required, at least on Windows. – Cameron May 14 '18 at 19:57
  • 1
    I concur with Cameron's assertion that the second `some_project` is required - at least on an AppVeyor CI build – Greg Trevellick Aug 22 '18 at 15:54
  • 1
    It still does not work. I have set the working directory by systemd, set `--prefix` and the install directory. npm still tries to install to /root/ – Julian F. Weinert Aug 25 '18 at 23:27
  • 10
    This doesn't seem to work for me on Windows 10. It installs the dependencies in the current directory and just puts a symlink in the subfolder. – Herohtar Aug 31 '18 at 04:04
  • 1
    Sometimes this does not create the node_modules/.bin directory. – Shaun Luttin Jan 24 '19 at 18:34
  • What if the package.json file is in a folder and we want the command to be executed outside that folder `../`? – M.suleman Khan Apr 04 '19 at 08:46
  • It is not exactly the same as `npm install` in the project directory, because of the symbolic links. – DAN Oct 23 '19 at 12:57
54

Update: Since the --prefix option exists, I now vote for @coudy's answer to this question. Original answer below:

No, npm will always install in the current directory or, with -g, in the system wide node_modules. You can kind of accomplish this with a subshell though, which won't affect your current directory:

(cd some_project && npm install)

The parentheses makes it run in a subshell.

Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
2

On Windows 10 I couldn't get --prefix to work, so I had to cd and execute it.

cd PATH_TO_FOLDER && npm install 
Yoannes Geissler
  • 791
  • 1
  • 9
  • 18
2

Create a package.json in the root directory with the following contents:

{
    "dependencies": {
        "helloworldprojectname": "file:hello\\world"
    }
}

Then call this to install:

npm install --prefix ./hello/world

It installs ./hello/world/node_modules using ./hello/world/package.json.

(Windows 10, Node v10.16.0, npm 7.6.1)

Aralox
  • 1,441
  • 1
  • 24
  • 44
1

On windows 10 using powershell the only thing that worked for me without all the problems and edge-cases mentioned in this blog post was this

Start-Process -Wait -FilePath "npm" -ArgumentList "install" -WorkingDirectory $web_dir
bottlenecked
  • 2,079
  • 2
  • 21
  • 32