Search code examples
node.jsservertsx.envenv-file

Node.js can't read .env


package.json:

"scripts": {
    "dev": "node --env-file=.env && tsx watch src/server.ts",

The server.ts is the entry point of the app, and it works if I remove the instruction "node --env-file=.env &&", but the reading of .env doesn't work either alone or combined with tsx command. What am I missing here?


Solution

  • You don't need to include node --env-file=.env in your dev script. Instead, just run the tsx command directly, as the environment variables will be loaded by dotenv in your server.ts file.

    "scripts": {
        "dev": "tsx watch src/server.ts"
    }