Ashwin
Ashwin

Reputation: 110

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. Running Windows 11 with Powershell & 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; rather, it installs my project as a package WTFF??

Whenever I run create-next-app, it also installs the dependencies to the global folder.

I've tried:

See below for screenshots.

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. But the workaround is to add --location=project every time I run an npm or npx command. 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

Upvotes: 0

Views: 1517

Answers (2)

riri
riri

Reputation: 21

A similar problem landed me here. I had a further look into the npm documentation and found something on there that helped me resolve the issue. It seems that if npm does not find a node_modules, or package.json it trawls your file system and uses a folder having any of these as your install directory.

Once I deleted some stray ones lurking further up it worked for me. Hope this helps.

Upvotes: 0

Ashwin
Ashwin

Reputation: 110

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. 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.

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.

Solution

  • Run npm config ls to see your configuration file
  • 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. However, to install global packages, you must add the --location=global flag every time you run the command.

Upvotes: 1

Related Questions