【问题标题】:NPM prefix flag does not work as expected on windowsNPM 前缀标志在 Windows 上无法按预期工作
【发布时间】:2021-04-06 02:03:32
【问题描述】:

我遇到了一个在 Mac 上不会发生但在 Windows 上会发生的问题。 我有一个项目,它放置文件并安装在刚刚放置的文件夹中,它运行的有效命令是“npm --prefix install”。然而,这会在 Windows 上导致错误:

npm ERR! code ENOLOCAL
npm ERR! Could not install from "" as it does not contain a package.json file.

这是完整的日志:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   '--prefix',
1 verbose cli   'C:\\Users\\jrjur\\Programs\\test\\',
1 verbose cli   'install' ]
2 info using npm@6.4.1
3 info using node@v11.1.0
4 verbose npm-session 59fa294aa88ba17f
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 silly fetchPackageMetaData error for file: Could not install from "" as it does not contain a package.json file.
8 timing stage:rollbackFailedOptional Completed in 2ms
9 timing stage:runTopLevelLifecycles Completed in 1102ms
10 verbose stack Error: ENOENT: no such file or directory, open 'C:\Users\jrjur\Programs\package.json'
11 verbose cwd C:\Users\jrjur\Programs
12 verbose Windows_NT 10.0.17134
13 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "--prefix" "C:\\Users\\jrjur\\Programs\\test\\" "install"
14 verbose node v11.1.0
15 verbose npm  v6.4.1
16 error code ENOLOCAL
17 error Could not install from "" as it does not contain a package.json file.
18 verbose exit [ 1, true ]

第 7 行似乎特别是发生错误的地方,但是我不知道“fetchPackageMetaData”是做什么的,也不知道它试图从哪个目录中读取...

放置的目录确实有一个 package.json,如果我 cd 进入该目录,我可以进行正常的 npm install 没有问题...同样,这个问题只在 Windows 上,确实如此在 Mac OS X 上不会发生。

如果有帮助,这是遇到问题的生成器(可以通过运行 npx tram-one-express test-project 重现问题):https://github.com/Tram-One/tram-one-express

【问题讨论】:

  • 另外,有这个确切的问题。我很惊讶它甚至还没有得到解决......

标签: node.js windows npm command-line-interface


【解决方案1】:

您需要在 --cwd 选项后添加相同的路径。

npm install --prefix --cwd

【讨论】:

    【解决方案2】:

    已经接受的答案是正确的,但它不适用于 linux。

    npm install --prefix your/path --cwd your/path
    

    这在 Windows 上运行良好,但在 linux 上它提供 ENOWORKSPACES: This command does not support workspaces

    要确保它对两者都有效,只需省略 --cwd 部分,如下所示:

    npm install --prefix your/path your/path
    

    【讨论】:

    • 但是,我不知道为什么--cwd 参数使“工作区”功能响应...
    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2014-10-20
    相关资源
    最近更新 更多