Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sumitbhanushali opened this issue Nov 23, 2023 · 9 comments
Open

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

sumitbhanushali opened this issue Nov 23, 2023 · 9 comments

Comments

@sumitbhanushali
Copy link

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
Copy link

Isn't it addressed in #2058 ?

@sumitbhanushali
Copy link
Author

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
Copy link

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
Copy link

t-rad679 commented Apr 17, 2024

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
Copy link

ICEatm commented Apr 25, 2024

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
Copy link

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

@ICEatm
Copy link

ICEatm commented May 2, 2024

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
Copy link

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
Copy link

ICEatm commented May 2, 2024

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants