为什么 npm 会在我的主目录中安装本地包? [英] Why does npm install local packages in my home directory?
问题描述
这里是 Node.js 新手,Windows 10.我在没有 的目录中
.npm 将包放在 npm install
-ed 一些包(没有 -g
)package.jsonC:\Users\{MyName}\node_modules\
中.
Node.js newbie here, Windows 10. I npm install
-ed some packages (without -g
) while inside a directory that didn't have package.json
. npm placed the packages in C:\Users\{MyName}\node_modules\
.
现在我看到了一些奇怪的行为:
Now I'm seeing some weird behavior:
- 当我在我的项目目录中时(有
package.json
但没有node_modules/
),npm list
和npm list -g
都显示一个空列表 - 当我在非项目目录中时(没有
package.json
)...npm list -g
仍然显示一个空列表- 但是,
npm list
显示了C:\Users\{MyName}\node_modules\
中的所有内容
- When I'm in my project directory (has
package.json
but nonode_modules/
yet),npm list
andnpm list -g
both show an empty list - When I'm in a non-project directory (no
package.json
)...npm list -g
still shows an empty list- However,
npm list
shows everything inC:\Users\{MyName}\node_modules\
问题 1. 这里发生了什么?显然,npm 的默认全局路径应该是
C:\Users\{MyName}\AppData\Roaming\npm\
.如果是这样,为什么它使用C:\Users\{MyName}\node_modules\
?Question 1. What is going on here? Apparently, npm's default global path should be
C:\Users\{MyName}\AppData\Roaming\npm\
. If so, why is it usingC:\Users\{MyName}\node_modules\
?问题 2. 我如何摆脱这个烂摊子?Node.js 从
C:\Users\{MyName}\node_modules\
导入包没有问题,但我希望 npm 正确列出它们.如何删除半全局包,正确重新安装它们,并确保不再发生这种情况?Question 2. How do I get out of this mess? Node.js has no problem importing packages from
C:\Users\{MyName}\node_modules\
, but I want npm to list them properly. How can I delete the semi-global packages, reinstall them correctly, and ensure that this doesn't happen again?推荐答案
好吧,原来我误将
npm install
-ing packages withoutpackage.json
.我第一次这样做时,我在我的主目录中(C:\Users\{MyName}\
).这导致 npm 在主目录中创建node_modules/
和package-lock.json
.进一步(错误地)尝试在我的项目中安装包 - 仍然缺少package.json
- 导致 npm 向上遍历,直到找到初始的node_modules/
目录,并在那里安装所有东西.因为我的主目录是 Node.js 寻找模块的地方之一,直到现在我才注意到我的错误.:PWelp, turns out I've been mistakenly
npm install
-ing packages withoutpackage.json
. The first time I did this, I was in my home directory(C:\Users\{MyName}\
). This caused npm to createnode_modules/
andpackage-lock.json
in the home directory. Further (mistaken) attempts to install packages in my projects--which were still missingpackage.json
--caused npm to traverse upwards, until it found the initialnode_modules/
dir, and install everything there. Because my home directory is among the places Node.js looks for modules, I didn't notice my mistake until now. :P这篇关于为什么 npm 会在我的主目录中安装本地包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!