Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active Jun 17, 2020
Embed
What would you like to do?
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