Node.js: Get (absolute) root path of installed npm package

x-ray

Task

I'm looking for an universal way to get the (absolute) root path of an installed npm package in Node.js.

Problem

I know about require.resolve, but that will give me the entry point (path to the main module) rather than the root path of the package.

Take bootstrap-sass as an example. Say it's installed locally in a project folder C:\dev\my-project. Then what I'm looking for is C:\dev\my-project\node_modules\bootstrap-sass. require.resolve('bootstrap-sass') will return C:\dev\my-project\node_modules\bootstrap-sass\assets\javascripts\bootstrap.js.

I can think of several methods how to get the package's root path:

Solution #1

var packageRoot = path.resolve('node_modules/bootstrap-sass');
console.log(packageRoot);

This will work fine for packages installed locally in node_modules folder. However, if I'm in a subfolder, I need to resolve ../node_modules/bootstrap-sass, and it get's more complicated with more nested folders. In addition, this does not work for globally installed modules.

Solution #2

var packageRoot = require.resolve('bootstrap-sass')
    .match(/^.*[\/\\]node_modules[\/\\][^\/\\]*/)[0];
console.log(packageRoot);

This will work for local and global modules installed in node_modules folder. The regex will match everything up to the last node_modules path element plus the following path element. However this will fail if a package's entry point is set to another package (e.g. "main": "./node_modules/sub-package" in package.json).

Solution #3

var escapeStringRegexp = require('escape-string-regexp');

/**
 * Get the root path of a npm package installed in node_modules.
 * @param {string} packageName The name of the package.
 * @returns {string} Root path of the package without trailing slash.
 * @throws Will throw an error if the package root path cannot be resolved
 */
function packageRootPath(packageName) {
    var mainModulePath = require.resolve(packageName);
    var escapedPackageName = escapeStringRegexp(packageName);
    var regexpStr = '^.*[\\/\\\\]node_modules[\\/\\\\]' + escapedPackageName +
        '(?=[\\/\\\\])';
    var rootPath = mainModulePath.match(regexpStr);
    if (rootPath) {
        return rootPath[0];
    } else {
        var msg = 'Could not resolve package root path for package `' +
            packageName + '`.'
        throw new Error(msg);
    }
}

var packageRoot = packageRootPath('bootstrap-sass');
console.log(packageRoot);

This function should work for all packages installed in a node_modules folder.

But...

I wonder if this rather simple task cannot be solved in a simpler and less hacky way. To me it looks like something that should already be built into Node.js. Any suggestions?

Tomas Kulich

Try this:

require.resolve('bootstrap-sass/package.json')

which returns:

path_to_my_project/node_modules/bootstrap-sass/package.json 

You can now get rid of 'package.json' path suffix such as:

var path = require('path') // npm install path
var bootstrapPath = path.dirname(require.resolve('bootstrap-sass/package.json'))

Since it is mandatory for every package to contain package.json file, this should always work (see What is a package?).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

npm install package in absolute path (Locally and package.json)

From Java

node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

From Dev

NODE_PATH matches the npm root

From Dev

Node.js reference path absolute

From Dev

Get installed version of npm from node script?

From Dev

Node JS NPM modules installed but command not recognized

From Dev

How to require an alternative js file from a npm package without specifiying full node_modules path

From Dev

Using stock node js package installer for OS X, global NPM bower path not recognized

From Dev

NPM: Why is this package installed?

From Dev

node js npm package OSX fails

From Dev

Node.js' npm is installing the wrong package

From Dev

Node require absolute path

From Dev

Node require absolute path

From Dev

How to get npm package to have working scripts in .bin when installed?

From Dev

Chocolatey installed package not on Path

From Dev

Node installed, but not NPM

From Dev

Subtract one absolute path from another in Node.js

From Dev

Node npm why all the modules are installed in the root node_modules folder?

From Dev

Node.js (npm) accessing files inside installed module

From Dev

Absolute Path from File in Package

From Dev

Get absolute path to File

From Java

Find the version of an installed npm package

From Dev

npm java package not getting installed

From Dev

npm - not installed; illegal characters in path

From Dev

node twitter npm package

From Dev

Does a Node.js package.json need to be at the root of the repository?

From Dev

With maven, how can I get the absolute path to where my root pom is?

From Dev

Express.js "path must be absolute or specify root to res.sendFile" error

From Dev

NPM not installed with the latest Node version

Related Related

  1. 1

    npm install package in absolute path (Locally and package.json)

  2. 2

    node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

  3. 3

    NODE_PATH matches the npm root

  4. 4

    Node.js reference path absolute

  5. 5

    Get installed version of npm from node script?

  6. 6

    Node JS NPM modules installed but command not recognized

  7. 7

    How to require an alternative js file from a npm package without specifiying full node_modules path

  8. 8

    Using stock node js package installer for OS X, global NPM bower path not recognized

  9. 9

    NPM: Why is this package installed?

  10. 10

    node js npm package OSX fails

  11. 11

    Node.js' npm is installing the wrong package

  12. 12

    Node require absolute path

  13. 13

    Node require absolute path

  14. 14

    How to get npm package to have working scripts in .bin when installed?

  15. 15

    Chocolatey installed package not on Path

  16. 16

    Node installed, but not NPM

  17. 17

    Subtract one absolute path from another in Node.js

  18. 18

    Node npm why all the modules are installed in the root node_modules folder?

  19. 19

    Node.js (npm) accessing files inside installed module

  20. 20

    Absolute Path from File in Package

  21. 21

    Get absolute path to File

  22. 22

    Find the version of an installed npm package

  23. 23

    npm java package not getting installed

  24. 24

    npm - not installed; illegal characters in path

  25. 25

    node twitter npm package

  26. 26

    Does a Node.js package.json need to be at the root of the repository?

  27. 27

    With maven, how can I get the absolute path to where my root pom is?

  28. 28

    Express.js "path must be absolute or specify root to res.sendFile" error

  29. 29

    NPM not installed with the latest Node version

HotTag

Archive