npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.46k stars 3.15k forks source link

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

Open pereorga opened 10 months ago

pereorga commented 10 months ago

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

Related: https://github.com/nodejs/node/issues/49514

ljharb commented 10 months ago

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

pereorga commented 10 months ago

thanks!

bengunnewijkGeonovum commented 8 months ago

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 https://github.com/nodejs/node/issues/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 commented 8 months ago

@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 commented 7 months ago

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 commented 5 months ago

up

bhuynhdev commented 2 days ago

Would love to see this