Why there is a node_modules folder under my home f

2020-07-24 02:10发布

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条回答
放荡不羁爱自由
2楼-- · 2020-07-24 02:39

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

查看更多
登录 后发表回答