How to get the location of the package installed globally

I have an npm package https://npmjs.com/package/@buildtip/create-web-app where the main file is running when the package is run is dist/cli.js. Now, this file needs to get a file from one upper directory. The method I implemented in node is

fs.readFile(`../generators/app/templates/${answer.stack}/package.json`,CB)

But this is checking from in the folder from where the command is running or from the location of the user’s terminal. Not from where this package is installed.
This is the code for the package. link

I want a node method or something which can get me the location of the project installed (global location as it is global package) not the location from where it is called.

(I have moved your topic to #community:javascript)

You can use __dirname which gives you directory of current module. Something like:

path.join(__dirname, `../generators/app/templates/${answer.stack}/package.json`)

https://nodejs.org/api/modules.html#modules_dirname

1 Like

Thanks Again . This one is working fine .

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.