cli icon indicating copy to clipboard operation
cli copied to clipboard

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

Open pereorga opened this issue 1 year ago • 6 comments
trafficstars

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

pereorga avatar Dec 10 '23 09:12 pereorga

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

ljharb avatar Dec 10 '23 17:12 ljharb

thanks!

pereorga avatar Dec 10 '23 18:12 pereorga

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);

bengunnewijkGeonovum avatar Feb 12 '24 16:02 bengunnewijkGeonovum

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

PaperStrike avatar Feb 15 '24 14:02 PaperStrike

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

  • https://github.com/nodejs/node/issues/51413
  • https://github.com/nodejs/node/pull/51476

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

penx avatar Mar 12 '24 10:03 penx

up

lucasdonato avatar May 22 '24 17:05 lucasdonato