xpt
xpt

Reputation: 22994

npm root: No such file or directory

What does npm root return?

I know here is how npm finds the node_modules directory:

Look for the module in the ./node_modules directory. If it is not there, recursively search in the parent directories’ ./node_modules until either the module is found or the root of the file system is reached.

Will npm root returns the same thing?

I have a node_modules directory up in a higher place, say /path/to/mynpmroot, but when doing npm root at a much lower place, say /path/to/mynpmroot/then/very/deep/here, the npm root gives me the path of /path/to/mynpmroot/then/very/deep/node_modules.

Is it normal? Because when I do ls /path/to/mynpmroot/then/very/deep/node_modules, I'll get:

ls: /path/to/mynpmroot/then/very/deep/node_modules: No such file or directory

This is annoying to me. but do I need to care? (as running node scripts within /path/to/mynpmroot/then/very/deep/here are just fine).

How could it happen, and how can I fix it?

Upvotes: 1

Views: 1785

Answers (1)

Elliot Nelson
Elliot Nelson

Reputation: 11557

npm root uses the folder returned by npm prefix, which searches for the closest package.json file.

Normally, you'll have a package.json file in /myproject/package.json; running npm root from /myproject/src/controllers should return /myproject/node_modules. If for some reason you have a package.json file closer (in your controllers folder... for some reason), that would explain the behavior.

This is using npm v6.0+; if you are seeing different behavior, make sure to mention what version you are using (npm -v).

Also note that if you are running npm prefix from a folder in a project without any package.json file at all, it will return just the current folder. That would also explain the behavior you are seeing.

Upvotes: 1

Related Questions