Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active June 17, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remarkablemark/57a43183decf58b6213e22f166b040f1 to your computer and use it in GitHub Desktop.
Save remarkablemark/57a43183decf58b6213e22f166b040f1 to your computer and use it in GitHub Desktop.
NAME=$npm_package_name
VERSION=${npm_package_version}

Example of npm environment variables loaded in a Node.js module.

Install

Clone repository:

git clone https://gist.github.com/57a43183decf58b6213e22f166b040f1.git npm-env-vars
cd npm-env-vars

Install dependencies:

npm install

Run

See npm environment variables:

npm start
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');
dotenvExpand(dotenv.config());
console.log(process.env.NAME, process.env.VERSION);
{
"name": "package-name",
"version": "1.2.3",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"dotenv": "^8.2.0",
"dotenv-expand": "^5.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment