TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.75k stars 529 forks source link

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

Open sumitbhanushali opened 7 months ago

sumitbhanushali commented 7 months ago

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

Ankur-Datta-4 commented 7 months ago

Isn't it addressed in #2058 ?

sumitbhanushali commented 7 months ago

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

nicholaspretorius commented 5 months ago

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

> simple-node@0.0.1 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.

t-rad679 commented 3 months ago

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.

ICEatm commented 2 months ago

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

andreabreu-me commented 2 months ago

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

ICEatm commented 2 months ago

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

andreabreu-me commented 2 months ago

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.

ICEatm commented 2 months ago

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"