you do not need to search for the package package.json file you need or even explicitly refer to its location.
Also, you shouldn't do this, as there is no sure way to find out where this npm package ends up in the npm tree (which is why you turned to require.resolve for help).
instead, you can request the npm (or CLI) API using npm ls using the --parseable flag, which will be:
Show syntax output instead of tree view.
eg:
$ npm ls my-dep -p
& hellip; will output something like this:
/Users/me/my-project/node_modules/my-dep
you should know that this command may display some irrelevant errors, as well as stderr (for example, about extraneous settings) - to get around this, activate the --silent flag (see loglevel in the docs):
$ npm ls my-dep -ps
this command can be integrated into your code using a child process , in which case it prefers to run the command without --silent to resolve any error.
if an error is detected, you can decide whether it will be fatal or not (for example, the aforementioned error about an extraneous package should be ignored).
so using CLI through a child process might look like this:
const exec = require('child_process').exec; const packageName = 'my-dep'; exec(`npm ls ${packageName} --parseable`, (err, stdout, stderr) => { if (!err) { console.log(stdout.trim());
this can be used (along with some closure magic & hellip;) in an asynchronous stream, for example:
const exec = require('child_process').exec; const async = require('async'); async.waterfall([ npmPackagePathResolver('my-dep'), (packagePath, callback) => { console.log(packagePath)