Sabbagh's Blagh
The Software blog of a Development Journeyman

No mon for `nodemon`

January 22, 2024

tagged: nodemon, frontend

Nodemon is always watching


Thanks to Alberto Rodríguez Santana for making this photo available freely on unsplash 🎁

If you're working on a several-years-old node project, you might run into the nodemon footgun we ran into last week.


We added an environment to the project for some shared work we were doing.

Our project uses dotenv for managing environments; to add a new one is pretty straightforward. Simply add an .env.<environment-name> with the environment variables you need, and set NODE_ENV.


This line of code does the loading of the specified environment:

  let config = { path: path.resolve(process.cwd(), 
    `.env.${process.env.NODE_ENV}`) };

  require('dotenv').config(config);

However, we were finding that the environment was always set to development, not our new one.


An Hour of debugging revealed the shocking truth: Nodemon was hijacking our environment! And to our dismay, we (actually the original developer) told it to do that!


Here's the nodemon.json:

{
  "restartable": "rs",
  "ignore": [".git", "node_modules/", "dist/", "coverage/"],
  "watch": ["**/*"],
  "execMap": {
    "ts": "node -r ts-node/register"
  },
  "env": {
    "NODE_ENV": "development"   // this bad boy right here
  },
  "ext": "js,json,ts"
},

Removing the env fixed the problem.


If you're bootstrapping a new node project, you won't need it anymore.

© Copyright 2024 Sabbagh's Blagh. Powered with by CreativeDesignsGuru