简体   繁体   English

默认情况下,npm install尝试全局安装

[英]npm install by default tries to install globally

When performing npm install on a folder, the package is installed on c:\\Users\\<user>\\node-modules instead of .\\<project folder>\\node-modules 在文件夹上执行npm install时,该软件包安装在c:\\Users\\<user>\\node-modules而不是.\\<project folder>\\node-modules

I've tried to update npm config save=false but this didn't solve the problem 我尝试更新npm config save = false,但这不能解决问题

PS C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp> npm install express
npm WARN danielk No description
npm WARN danielk No repository field.
npm WARN danielk No license field.

+ express@4.17.1
updated 1 package and audited 126 packages in 2.004s
found 0 vulnerabilities

PS C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp> dir


    Directory: C:\Users\danielk\Documents\udemy_nodejs\FirstExpressApp


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       21/08/2019   7:54 AM             27 app.js

When doing npm install express in the project folder, I would expect the node-modules subfolder to be created in the project folder and the express module to be installed in the node-modules subfolder. 在项目文件夹中执行npm install express时,我希望在项目文件夹中创建node-modules子文件夹,而express模块​​将安装在node-modules子文件夹中。 However its created in C:\\Users\\danielk\\node-modules. 但是,它是在C:\\ Users \\ danielk \\ node-modules中创建的。

Could anyone help on what is wrong and how this can be fixed? 任何人都可以提供帮助以解决问题以及如何解决该问题吗?

When you do npm install, npm actually installs the package to the "npm project" you are currently in. See the following example: 当您执行npm install时,npm实际上将软件包安装到您当前所在的“ npm project”中。请参见以下示例:

eric_@Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (stackoverflow)
[...Truncated...]
Is this OK? (yes)

eric_@Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ mkdir child

eric_@Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow
$ cd child/

eric_@Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ npm install express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN stackoverflow@1.0.0 No description
npm WARN stackoverflow@1.0.0 No repository field.

+ express@4.17.1
added 50 packages from 37 contributors and audited 126 packages in 3.021s
found 0 vulnerabilities


eric_@Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls

eric_@Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls ..
child/  node_modules/  package.json  package-lock.json

eric_@Eric-Dev-Laptop MINGW64 ~/Desktop/mixed/stackoverflow/child
$ ls ../node_modules/
accepts/              escape-html/        mime/            safer-buffer/
array-flatten/        etag/               mime-db/         send/
body-parser/          express/            mime-types/      serve-static/
bytes/                finalhandler/       ms/              setprototypeof/
content-disposition/  forwarded/          negotiator/      statuses/
content-type/         fresh/              on-finished/     toidentifier/
cookie/               http-errors/        parseurl/        type-is/
cookie-signature/     iconv-lite/         path-to-regexp/  unpipe/
debug/                inherits/           proxy-addr/      utils-merge/
depd/                 ipaddr.js/          qs/              vary/
destroy/              media-typer/        range-parser/
ee-first/             merge-descriptors/  raw-body/
encodeurl/            methods/            safe-buffer/

What happens is, I made a stackoverflow folder, initialized an "npm project" there with npm init , and cd into child folder. 发生的是,我制作了一个stackoverflow文件夹,并使用npm init初始化了一个“ npm项目”,并将cd放入child文件夹。 When I do npm install express inside, the express module will inadvertently get installed to stackoverflow/node_module . 当我在内部执行npm install expressexpress模块将无意中安装到stackoverflow/node_module This is such that when you are making a program, eg myprogram, and even when you are in some subfolder inside (eg myprogram/lib/ ) and you perform npm install , the module will still be installed to myprogram. 这样一来,当您制作程序(例如myprogram)时,即使您位于其中的某个子文件夹中(例如myprogram/lib/ )并且执行npm install ,该模块仍将安装到myprogram中。

Comparing this to your case, this is probably because your C:\\Users\\danielk\\ is already an npm project, so when you are in C:\\Users\\danielk\\Documents\\udemy_nodejs\\FirstExpressApp , npm thinks you are in the C:\\Users\\danielk\\ project and thus save the express module there. 将此与您的情况进行比较,这可能是因为您的C:\\ Users \\ danielk \\已经是一个npm项目,所以当您在C:\\ Users \\ danielk \\ Documents \\ udemy_nodejs \\ FirstExpressApp中时,npm会认为您在C:中。 \\ Users \\ danielk \\项目,因此将express模块保存在那里。

As for why C:\\Users\\danielk\\ became an npm project, is it either you manually did npm init there before, or you performed your first npm install there, and C:\\Users\\danielk\\node_modules is created, marking it an npm project. 至于为什么C:\\ Users \\ danielk \\成为npm项目,是您之前在那里手动执行了npm init ,还是在那里执行了首次npm install ,并创建了C:\\ Users \\ danielk \\ node_modules,将其标记为npm项目。

And one more thing, that express is not installed "globally", you do "global" install by npm install express -g (well though for express there is no use to install it globally). 还有一件事情是,express不是“全局”安装的,您可以通过npm install express -g进行“ global”安装(尽管对于express没有使用全局安装)。 Your case is just the package getting "installed in the home directory". 您的情况只是该软件包“安装在主目录中”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM