Why is there a node_modules folder under my home folder?
Asked Answered
M

2

6

I already have a global node_modules folder in /usr/local/lib/node_modules, but I just found there is also a ~/node_modules folder under my home folder. Can I delete this one?

I execute node -e "console.log(global.module.paths)" and I get:

[ '/Users/Username/node_modules',
'/Users/node_modules',
 '/node_modules' ]

And if I delete the node_modules folder, which is under the home directory, then I execute npm list @vue/cli-ui. It would should this error:

/Users/Username
└── UNMET DEPENDENCY @vue/[email protected]
npm ERR! missing: @vue/[email protected], required by Username

So, can I delete the node_modules folder under my home directory? What's the use of it? Or should I need reinstall Node.js and npm?

And if I do delete this folder, when I execute npm ls, I would get these errors:

/Users/Username
├─┬ UNMET DEPENDENCY @vue/[email protected]
│ ├─┬ UNMET DEPENDENCY @akryum/[email protected]
│ │ └── UNMET DEPENDENCY [email protected]
│ ├─┬ UNMET DEPENDENCY @vue/[email protected]
│ │ ├── UNMET DEPENDENCY [email protected]
│ │ ├── UNMET DEPENDENCY [email protected]
│ │ ├─┬ UNMET DEPENDENCY [email protected]
│ │ │ ├── UNMET DEPENDENCY [email protected]

How can I solve this problem?


Now everything is OK after executing npm cache verify.

Mydriatic answered 27/11, 2018 at 9:57 Comment(0)
L
11

module.paths are the paths where Node.js search for NPM packages; and it actually doesn't search in your NPM global directory, as you can see.

More information is in Loading from the global folders and in All together....

You see those paths because you're executing node -e ... when you are in home directory, the Node.js simply traverse all node_modules paths to the root.

'/Users/node_modules',
 '/node_modules' ]

Relating to your question: Yes, you can delete ~/node_modules; probably it's there because you once wrote npm i MODULE without -g flag and your current working directory was ~.

Lissettelissi answered 27/11, 2018 at 10:22 Comment(7)
I delete this folder and I get some errors and I update it on my question. How to solve this problem? Thanks.Mydriatic
Delete the package.json in your home directory. He's reading that file and obviously he doesn't find anything because you just deleted the node_modules.Lissettelissi
To deal with global modules, use npm -g (ex: npm -g ls)Lissettelissi
The weired thing is that there is no package.json or package-lock.json file at my home folder. So I don’t know why it would show me these errors after I delete the node_modules folder.Mydriatic
What node version?Lissettelissi
npm version is 5.6.0 and node version is 8.11.4Mydriatic
Let us continue this discussion in chat.Lissettelissi
I
0

After years of using node, I've idiotically installed it everywhere on my Windows machine.

  • Delete all "path\to\node_modules" folder and the "path\to\node_modules\ ..\package.json". In other words, remove node_modules, package.json, yarn, etc that are located in weird places (credits to kopiro).

  • Make sure you only have one copy of node on your machine at a time.

  • If you want multiple copies, I recommend to use NVM to manage multiple versions for node in both WSL and Windows. There will be less confusion.

  • Chocolatey is very nice for installing cli apps like nvm https://community.chocolatey.org/packages/nvm

Good luck!

Innings answered 4/3 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.