For my web application, when I try to import a version tag from package.json to app.js (/src/app.js), which is one level up to app.js, the local run works fine. But when I try to run from development by generating a dist folder, the code does not work even though both src and dist folder are at same level and should be able to access package.json.
When I deploy to development, I get error as follows:
Error: Cannot find module '../package.json'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
Can someone pls help me with this?
If you're using Node.js, which seems to be the case, use node native
path
module to resolvepackage.json
path. Here is an example :package.json
foo.js
index.js
When your run
node index
, you'll get the following outputThe version from
package.json
is read from bothindex.js
andfoo.js
usingpath.resolve('package.json')