简体   繁体   中英

Find the file system location of an installed NodeJS module

I'm looking at the below code from the grunt-template-jasmine-istanbul project on GitHub;

var DEFAULT_TEMPLATE = __dirname + '/../../../../grunt-contrib-jasmine/tasks/'
                       + 'jasmine/templates/DefaultRunner.tmpl';

I've noticed before that you can (not that you necessarily should) require internals from a NodeJS module like so;

console.log(require('grunt/package.json'));

But in this case the file is an Underscore.js template, so you can't require it.

Is there a way to find the install location (which can differ) of an npm module in order to harden this a little?

require.resolve(moduleName)

should do the trick for locally installed node_modules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM