ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

add --env-file environment variable similar to node v:20.6.0

Open sumitbhanushali opened this issue 2 years ago • 9 comments

Desired Behavior

since v:20.6.0, nodejs natively supports .env files for loading environment variables. I use ts-node for development purposes and intend to not use dotenv package.

Is this request related to a problem?

Alternatives you've considered

Additional context

sumitbhanushali avatar Nov 23 '23 20:11 sumitbhanushali

Isn't it addressed in #2058 ?

Ankur-Datta-4 avatar Nov 24 '23 21:11 Ankur-Datta-4

Isn't it addressed in #2058 ?

This solves my issue, Thanks. But, I still think there should be straightforward way to do this like

ts-node --env-file=config.env app.ts

sumitbhanushali avatar Nov 25 '23 07:11 sumitbhanushali

I believe this is a valid issue.

As mentioned, #2508 provides a workaround, however, --env-file config option does not seem to be supported in ts-node yet.

ts-node throws an error that says "code: 'ARG_UNKNOWN_OPTION'" when using --env-file.

See error below from running a simple index.js file with a PORT env variable.

% npm run dev:ts

> [email protected] dev:ts
> ts-node --env-file=.env ./src/index.ts

/simple-node/node_modules/arg/index.js:90
                                                throw err;
                                                ^

Error: Unknown or unexpected option: --env-file
    at arg (/simple-node/node_modules/arg/index.js:88:19)
    at parseArgv (/simple-node/node_modules/ts-node/dist/bin.js:69:12)
    at main (/simple-node/node_modules/ts-node/dist/bin.js:25:18)
    at Object.<anonymous> (/simple-node/node_modules/ts-node/dist/bin.js:579:5)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'ARG_UNKNOWN_OPTION'
}

Node.js v20.11.0

The Node.js feature itself was released in Node.js v20.6.0, and I am running Node.js v20.11.0 with ts-node v10.9.2.

nicholaspretorius avatar Feb 15 '24 10:02 nicholaspretorius

I'm seeing this issue as well, except the workaround (-r ts-node/register) does not seem to work for me. The oddest thing is that it seemed to be working and then suddently stopped.

t-rad679 avatar Apr 17 '24 05:04 t-rad679

I'm seeing this issue as well, except the workaround (-r ts-node/register) does not seem to work for me. The oddest thing is that it seemed to be working and then suddently stopped.

For me the --env-file flag also does not work.. however as you stated using ts-node/register it works perfectly fine for me. "start:dev": "node -r ts-node/register --env-file=.env ./src/index.ts" <-- that is my script.

My node version is v20.8.1 ts-node version is ^10.9.2

h4or avatar Apr 25 '24 09:04 h4or

nodemon seems to proxy to ts-node as ts-node --env-file=.env src/app.ts

abreu-dev avatar May 01 '24 16:05 abreu-dev

nodemon seems to proxy to ts-node as ts-node --env-file=.env src/app.ts

You could also use the --exec flag in nodemon and then type your script as you would do normally.. but idk if this changes anything

h4or avatar May 02 '24 10:05 h4or

Thanks @ICEatm. How the --exec argument can pass the --env-file arg to node?

So far what is working for me: nodemon -r dotenv/config src/main.ts but had to add dotenv as dev dependency.

abreu-dev avatar May 02 '24 15:05 abreu-dev

Thanks @ICEatm. How the --exec argument can pass the --env-file arg to node?

So far what is working for me: nodemon -r dotenv/config src/main.ts but had to add dotenv as dev dependency.

Using the --exec flag you can then pass other normal npm commands to nodemon. This is what it could look like then "start:test": "nodemon --exec node -r ts-node/register --env-file=.env ./src/index.ts"

h4or avatar May 02 '24 15:05 h4or