user1394625 user1394625 - 2 months ago 15
Node.js Question

Node.js how to find local node_modules folder from a globally installed package

I'm writing my own CLI node module 'npak' that is installed globally.

I need to find the absolute path of the local 'node_modules' folder of a project where the (the global) cli command was executed.

say the project looks like this:

/home
/projects
/test-project/
/package.json
/node-modules
/folder-one
/folder-two
/folder-three/ npak find-modules
^-- this should return absolute path of node-modules folder


should return : /home/projects/test-project/node-modules even though npak is installed globally

How do I get at the absolute path of the local 'node-modules' folder regardless of what the cwd is from a globally installed package?

Answer

There are some useful modules, like find-node-modules, that can help you with such a task.