Why there is a node_modules folder under my home folder?

徘徊边缘 提交于 2019-12-08 04:07:28

问题


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 floder. 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 home directory, then I execute npm list @vue/cli-ui , It would should this error:

/Users/Username
└── UNMET DEPENDENCY @vue/cli-ui@3.0.1 
npm ERR! missing: @vue/cli-ui@3.0.1, required by Username

So, can I delete the node_modules foleder under my home directory? What's the use of it? Or should I need re-install node and npm?

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

/Users/Username
├─┬ UNMET DEPENDENCY @vue/cli-ui@3.0.1
│ ├─┬ UNMET DEPENDENCY @akryum/winattr@3.0.0
│ │ └── UNMET DEPENDENCY fswin@2.17.1227
│ ├─┬ UNMET DEPENDENCY @vue/cli-shared-utils@3.0.1
│ │ ├── UNMET DEPENDENCY chalk@2.4.1
│ │ ├── UNMET DEPENDENCY execa@0.10.0
│ │ ├─┬ UNMET DEPENDENCY joi@13.6.0
│ │ │ ├── UNMET DEPENDENCY hoek@5.0.4

How to solve this problem?

Now everything is ok after executing npm cache verify


回答1:


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

More info here https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders and here https://nodejs.org/api/modules.html#modules_all_together.

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

[ '/Users/Username/node_modules', '/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 cwd was ~.



来源:https://stackoverflow.com/questions/53497035/why-there-is-a-node-modules-folder-under-my-home-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!