Why does npm install local packages in my home directory?

Phi*_*ang 2 node.js npm

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:

  • When I'm in my project directory (has package.json but no node_modules/ yet), npm list and npm 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 in 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 using C:\Users\{MyName}\node_modules\?

问题2:如何摆脱困境?Node.js从中导入包没有问题C:\Users\{MyName}\node_modules\,但我希望npm正确列出它们。如何删除半全局软件包,正确地重新安装它们,并确保不再发生这种情况?

Phi*_*ang 8

Welp,结果证明我一直在错误地npm install打包不含的 软件包package.json。第一次执行此操作时,我位于主目录(C:\Users\{MyName}\)中。这导致npm创建node_modules/package-lock.json位于主目录中。进一步(错误)尝试在我的项目中安装软件包(这些软件包仍然丢失),package.json导致npm向上遍历,直到找到初始node_modules/目录,然后在其中安装所有内容。因为我的主目录位于Node.js查找模块的地方,所以直到现在我才注意到自己的错误。:P