简体   繁体   中英

How to resolve path to a file within node_modules

Problem loading a file using relative path when my node app is initialised using a another node app

I have created an npm which relies on a file stored relative to project root. something like this

index.js
- res 
  - config.json

Now I read the config.json using following code

const pathToConfig =  path.resolve(__dirname, '../res/config.json')

This works great locally.
But in my prod setup this app is initialised by another node app.
And __dirname resolves to root of that app so all my logic to find config.json get messed up.
Is there any way I can read the file without worrying about how node app was initialised?

Have you tried the command process.cwd()? It is almost the same as __dirname but does differ slightly.

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