Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query the application name in nodejs

Tags:

node.js

How can I access the name field from package.json at runtime?

Is that already stored somewhere or should I open and parse package.json myself?

like image 894
fortran Avatar asked Dec 01 '22 15:12

fortran


2 Answers

If you start your application with npm, a more robust & more simple approach consists in using process.env.npm_package_name.

See https://docs.npmjs.com/misc/scripts#packagejson-vars for more information.

like image 150
Jean-Francois Brazeau Avatar answered Dec 06 '22 12:12

Jean-Francois Brazeau


It's not stored anywhere by default; the easiest way to load it is probably to just use require:

require(__dirname + '/package.json').name
like image 40
josh3736 Avatar answered Dec 06 '22 11:12

josh3736