简体   繁体   English

Node.js 不会在项目文件夹中安装包

[英]Node.js will not install packages in project folder

I'm not sure what happened, but I've wasted six hours trying to fix Node.js as it just stopped working out of nowhere.我不确定发生了什么,但我已经浪费了六个小时试图修复 Node.js,因为它突然停止工作。 Running Windows 11 with Powershell & Git Bash .运行Windows 11Powershell & Git Bash

When I try to run npm install in my local git repository with a Next.js application ( which I already initialized and even deleting and initializing again ), it does not work;当我尝试在本地 git 存储库中使用 Next.js 应用程序(我已经初始化甚至再次删除和初始化)运行npm install时,它不起作用; rather, it installs my project as a package WTFF??相反,它将我的项目安装为 package WTFF??

Whenever I run create-next-app, it also installs the dependencies to the global folder.每当我运行create-next-app,它还会将依赖项安装到全局文件夹中。

I've tried:我试过了:

  • Reinstalling Node.js (via Winget and Scoop)重新安装 Node.js(通过 Winget 和 Scoop)
  • Removing node_modules folder globally全局删除node_modules文件夹
  • Deleting package-lock.json删除 package-lock.json
  • Creating a new project ( doesn't work on any repository )创建一个新项目(不适用于任何存储库
  • Configure --location flag at default startup to project , but it still did not fix it.在默认启动时将--location标志配置为project ,但它仍然没有修复它。
  • Deleting old package.json and initalizing a new one删除旧的 package.json 并初始化一个新的
  • Creating a new project创建一个新项目

See below for screenshots.请参阅下面的屏幕截图。

  • What it says every time I run npm install in any repository每次我在任何存储库中运行npm install时它说的是什么每次我在任何存储库中运行 npm install 时它说什么

  • Result of npm list npm list的结果npm 列表的结果

  • Modified Configuration file修改后的配置文件在此处输入图像描述

Please let me know if anyone has any idea, whether right or wrong.请让我知道是否有人有任何想法,无论是对还是错。 I tried looking online for related posts but couldn't find any like mine;我试着在网上寻找相关的帖子,但找不到像我这样的帖子; if I overlooked any, please link them below.如果我忽略了任何内容,请在下面链接它们。 Thanks to anyone that helps, I have been losing my mind over this.感谢任何提供帮助的人,我对此一直失去理智。

EDIT: When I set the flag for npm in the npm shim files, it does not work.编辑:当我在 npm 填充文件中为 npm 设置标志时,它不起作用。 But the workaround is to add --location=project every time I run an npm or npx command.但解决方法是每次运行 npm 或 npx 命令时添加--location=project I know you can set an alias to make life easier.我知道你可以设置一个别名来让生活更轻松。 How do I fix the configuration files?如何修复配置文件?

I edited the shims located in: C:\Program Files\nodejs\node_modules\npm\bin and C:\Program Files\nodejs我编辑了位于以下位置的垫片: C:\Program Files\nodejs\node_modules\npm\binC:\Program Files\nodejs

I found out how to fix this problem;我发现了如何解决这个问题; it had to do with a .npmrc already existing in my User folder, which automatically added it to the npm config, or I'm assuming.它与我的用户文件夹中已经存在的.npmrc有关,它自动将其添加到 npm 配置中,或者我假设。 All I had to do with remove the .npmrc file in my home directory and run npm config set location=project , and it configured itself to install to the project directory by default.我所要做的就是删除主目录中的.npmrc文件并运行npm config set location=project ,它默认将自身配置为安装到项目目录。

Problem问题

.npmrc file existed when it should not have been there, so even though I reinstalled Node, the .npmrc never got deleted, so it always defaulted to the old configuration file and never worked. .npmrc文件在它不应该存在的时候就存在了,所以即使我重新安装了 Node, .npmrc也从未被删除,所以它总是默认为旧的配置文件并且从未工作过。

Solution解决方案

  • Run npm config ls to see your configuration file运行npm config ls查看你的配置文件
  • For me, there was a line in the configuration that looked like对我来说,配置中有一行看起来像
    ; "global" config from C:\Users\Ashwi\.npmrc
  • I deleted the file, then updated the configuration by setting the default location to project我删除了文件,然后通过将默认位置设置为项目来更新配置
    npm config set location=project
  • Then I restarted Powershell, and everything worked perfectly.然后我重新启动了 Powershell,一切正常。 However, to install global packages, you must add the --location=global flag every time you run the command.但是,要安装全局包,您必须在每次运行命令时添加--location=global标志。

Use diffrent drive then c(sytem drive) in Windows OS.在 Windows OS 中使用不同的驱动器,然后使用 c(系统驱动器)。

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

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