Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENHANCEMENT] Implement --env-file option in npm-run-script #7069

Open
pereorga opened this issue Dec 10, 2023 · 9 comments
Open

[ENHANCEMENT] Implement --env-file option in npm-run-script #7069

pereorga opened this issue Dec 10, 2023 · 9 comments

Comments

@pereorga
Copy link

See equivalent Node.js option in https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--env-fileconfig

Related: nodejs/node#49514

@ljharb
Copy link
Contributor

ljharb commented Dec 10, 2023

There’s no need; set NODE_OPTIONS to contain that node option.

@pereorga
Copy link
Author

thanks!

@bengunnewijkGeonovum
Copy link

Based on @ljharb's comment that it should work with just setting NODE_OPTIONS I expected method 1 and 2 to work, but it only worked with method 3.

// Method 1
{
  ...,
  "scripts": {
    "build": "set NODE_OPTIONS='--env-file=.env' && webpack --config webpack.config.js"
  },
  ...
}

Another method I expected to work but didn't:

// Method 2
set NODE_OPTIONS='--env-file=.env'
npm run build

The method that ended up working for me, thank you meduzen in nodejs/node#49514

// Method 3
{
  ...,
  "scripts": {
    "build": "node --env-file=.env node_modules/webpack/bin/webpack --config webpack.config.js"
  },
  ...
}

In webpack.config.js I have two console logs:
console.log("A .env value:");
console.log(process.env.MY_VAR);

@PaperStrike
Copy link

@pereorga @ljharb Did NODE_OPTIONS work? I am getting: --env-file= is not allowed in NODE_OPTIONS. It's not listed in the documentation.

@penx
Copy link

penx commented Mar 12, 2024

There’s no need; set NODE_OPTIONS to contain that node option.

@ljharb can this be reopened? As per comments above, this is not an option. In latest node LTS (20.11.1) and current (21.7.1) you get

node: --env-file is not allowed in NODE_OPTIONS

Also see

Now that process.loadEnvFile and util.parseEnv are available, perhaps this can be implemented as a cli flag in npx?

@lucasdonato
Copy link

up

@bhuynhdev
Copy link

Would love to see this

@ericgopak
Copy link

up 👍

@checkmatez
Copy link

It can be useful to run a single npm script both locally and in CI/CD, where the CI/CD environment variables are injected automatically, while local variables are loaded from the .env file:

"test:db": "jest db.test.js",

Now you can run it locally: `npm run --env-file=.env test:db" while in CI/CD you would use a built-in mechanism for passing environment variables to scripts and run it simply with "npm run test:db".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants