Search code examples
node.jsdockerpackage.jsonnodemonnpm-scripts

Nodemon not working using npm script from package.json on Docker


I'm working with NodeJS and Nodemon on Docker. When I try to run my NodeJS app using nodemon command directly in docker compose file, it runs.

Like this (working): [docker-compose]

command: nodemon source/index.js

But when I use a script from package.json, it doesn't work

Like this (not-working): [docker-compose]

command: npm run dev

Where my package.json file is

"scripts": {
    "start": "node source/index.js",
    "dev": "nodemon source/index.js"
  }

I tried different things, when I simply run start script without nodemon, it works

Like this (working): [docker-compose]

command: npm run start

But when I try to use dev again with nodemon command inside it, it doesn't work. Container won't start. I have also tried the following and it also works

Like this (working): [docker-compose]

command: nodemon --exec npm start

I still don't understand, why nodemon command is not working inside script dev

I'm using Docker in Swarm Mode

Here are my both files

docker-compose

version: '3.7'

services:

    node-service:

        image: node-img:1.0

        ports:
        - 4000:4000

        working_dir: "/node-dir"

        volumes:
        - ./node-dir/source:/node-dir/source

        networks:
            - ness-net

        command: npm run dev

networks:

    ness-net:

package.json

{
  "name": "node-pkg",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node source/index.js",
    "dev": "nodemon source/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "nodemon": "^1.19.4"
  }
}

Solution

  • Just add the "." to define the path in your package.json like this

    "scripts": {
        "start": "node ./source/index.js",
        "dev": "nodemon ./source/index.js"
      }