Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run script from PNPM bin with env-file

Tags:

node.js

pnpm

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.

like image 928
madflow Avatar asked May 30 '26 10:05

madflow


1 Answers

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

like image 193
imme Avatar answered Jun 01 '26 23:06

imme