get_node_modules_path

    2.0.4 • Public • Published

    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"{
      "build": "cp -R `node_modules`/font-awesome/fonts/* dist/fonts/", <=== this will be executed as cp -R /path-to-your-node_modules/or-the-node_modules-of-a-parent/font-awesome/fonts/* dist/fonts/

    With a specific module name as a param:

    "scripts"{
      "test": "echo  `node_modules` can differ from `node_modules module1`", <=== this will be executed as cp -R /path-to-your-node_modules/or-the-node_modules-of-a-parent/font-awesome/fonts/* dist/fonts/
     

    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.use('/fonts', express.static(Path.resolve(node_modules(), 'font-awesome/fonts/')));
    // 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.use('/fonts', express.static(Path.resolve(node_modules('font-awesome'), 'font-awesome/fonts/')));
    // node_modules('font-awesome') will return the path to your node_modules or to the node_modules of a parent

    Install

    npm i get_node_modules_path

    DownloadsWeekly Downloads

    8

    Version

    2.0.4

    License

    GPL

    Unpacked Size

    6.34 kB

    Total Files

    4

    Last publish

    Collaborators

    • channg