I have an .env file:
STH=1
In Node 20 I can do:
node --env-file=.env -e "console.log(process.env.STH)"
1
Is there a way, I can incorporate this into running a script with "PNPM". For illustration (I do not want actually to run prettier) with Prettier in the package.json:
pnpm prettier
Usage: prettier [options] [file/dir/glob ...]
Is there a way I can pre-process a .env file by using the new NodeJs --env-file feature and use the result with a pnpm script? In the "Prettier" example the prettier script would have access to all env variables defined in .env. --env-file is not allowed in NODE_OPTIONS.
As mentioned, adding --env-file as value to node-options in the (workspace) .npmrc does not work. This also unlikely to be supported as .env is interpretable as a "stale" version of the environment variables and NODE_OPTIONS is such an environment variable.
Until pnpm adds support for something like --env-file, using a dependency such as @dotenvx/dotenvx, dotenv, cross-env or something like that seems to be the only easier option.
Redoing what those projects did in custom code is another option. That custom code can than be "injected" into node's options with node-options=--require=custom_dotenv.cjs (or --import).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With