为什么 npm 会在我的主目录中安装本地包? [英] Why does npm install local packages in my home directory?

查看:46
本文介绍了为什么 npm 会在我的主目录中安装本地包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是 Node.js 新手,Windows 10.我在没有 的目录中npm install-ed 一些包(没有 -g)package.json.npm 将包放在 C:\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 listnpm 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 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\

      问题 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 using C:\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 without package.json.我第一次这样做时,我在我的主目录中(C:\Users\{MyName}\).这导致 npm 在主目录中创建 node_modules/package-lock.json.进一步(错误地)尝试在我的项目中安装包 - 仍然缺少 package.json - 导致 npm 向上遍历,直到找到初始的 node_modules/ 目录,并在那里安装所有东西.因为我的主目录是 Node.js 寻找模块的地方之一,直到现在我才注意到我的错误.:P

      Welp, turns out I've been mistakenly npm install-ing packages without package.json. The first time I did this, I was in my home directory(C:\Users\{MyName}\). This caused npm to create node_modules/ and package-lock.json in the home directory. Further (mistaken) attempts to install packages in my projects--which were still missing package.json--caused npm to traverse upwards, until it found the initial node_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆