admin管理员组

文章数量:1410697

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 mand, say for example the npm install mand 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 mands 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?

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 mand, say for example the npm install mand 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 mands 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?

Share Improve this question asked Feb 14, 2019 at 5:50 Water Cooler v2Water Cooler v2 33.9k63 gold badges183 silver badges365 bronze badges 3
  • npm start --prefix 'path/to/your/package.json' – Helping hand Commented Feb 14, 2019 at 5:55
  • Thank you. It appears as though you don't need the file name; just the folder name will do. Supplying the file name package.json will produce an error. – Water Cooler v2 Commented Feb 14, 2019 at 6:10
  • 1 oh yeah, what I meant was the path to package.json file only. Anyways glad it worked. – Helping hand Commented Feb 14, 2019 at 11:10
Add a ment  | 

2 Answers 2

Reset to default 4

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/

Athough HelpingHand in the ments 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 mand and specify a path to the package.json file:

$ npm install <folder_path>

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

To run any other npm mand, 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 mand 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.

本文标签: javascriptTell npm to look for the packagejson in a specific folderStack Overflow