Water Cooler v2
Water Cooler v2

Reputation: 33850

Tell npm to look for the package.json in a specific folder

Let's for the sake of simplicity, say that I have a folder structure like so:

root
|-build
  |-package.json
|-src
  |-foo
    |-foo.csproj
    |-foo.cs
    |-bar.cs
    |-bin
      |-...
  |-foo.sln

Let us then say that I change the current directory to root\src\foo\bin and execute any npm command, say for example the npm install command like so:

C:\root\src\foo\bin> npm install

We will observe that npm will start looking for the package.json file within the current directory, and since it won't find it, will report an error like so:

npm ERR! path C:\root\src\foo\bin\package.json
npm ERR! code ENOENT
1>  npm ERR! errno -4058
1>  npm ERR! syscall open
1>  npm ERR! enoent ENOENT: no such file or directory, open 'C:\root\src\foo\bin\package.json'
1>  npm ERR! enoent This is related to npm not being able to find a file.
1>  npm ERR! enoent 

In this case, supposing I had a limitation that I had to execute all commands from within the root\src\foo\bin\ folder, how would I tell npm to look for the package.json file that's in the root\build\ folder?

Upvotes: 2

Views: 7819

Answers (2)

Water Cooler v2
Water Cooler v2

Reputation: 33850

Athough HelpingHand in the comments section to the question, and pavan skipo in the answers have provided the correct answers, I am writing this answer to consolidate what knowledge I have acquired about solving this issue as I got the answer to this question by searching some more soon after having posted this question.

To run the npm install command and specify a path to the package.json file:

$ npm install <folder_path>

$ npm install "..\..\..\build\"

To run any other npm command, for example an npm script and specify a path to the package.json file:

$ npm --prefix <folder_path> run <script_name>

For e.g if the package.json had a script named build, the command would be:

$ npm --prefix "..\..\..\build" run build

Note that:

  1. The trailing slash \ (or \) after the folder name is optional. So ..\..\..\build\ is the same as ../../../build/, which is the same as ..\..\..\build.

  2. <folder_path> is the path of the folder that contains the package.json that you want npm to look for.

Upvotes: 0

Pavan Skipo
Pavan Skipo

Reputation: 1857

Just to ensure i understood your question right, you are in root directory and package.json is in build sub-directory and you want to install packages from root directory, right? if so

You can give npm install <folder_path> so in your case from root you can give npm install build/

Upvotes: 4

Related Questions