Get the path of the node_modules
folder in your scripts or CLI or package.json
. This is useful when you are building a library that can either be used as an npm dependency or directly, see this question on SO.
You can add a parameter in order to look for a specific module. This is useful when npm creates multiple node_modules folder for conflicts reasons.
Installation
$ npm install node_modules-path --save
Test
$ npm test
This project has been battle tested on Silex website builder
Access node_modules in package.json
Use it in your packge.json
like this:
"name": "my-super-project","scripts":
With a specific module name as a param:
"scripts":
node_modules folder path from another nodejs script
const node_modules = require('node_modules-path');
console.log('node module path for this project:', node_modules());
This is especially useful to serve fonts in an express app
app;// node_modules() will return the path to your node_modules or to the node_modules of a parent
With a specific module name as a param, which is safer:
app;// node_modules('font-awesome') will return the path to your node_modules or to the node_modules of a parent