Docker - /bin/sh: nodemon: not found

Viewed 1463

I have the following docker file.

#Dockerfile

FROM node:alpine

WORKDIR /backend

COPY package*.json .

RUN yarn

COPY . .

EXPOSE 5000

CMD ["yarn", "dev"]

Nodemon works as intended on host machine but shows the following error message when running the container.

yarn run v1.22.5
$ nodemon
/bin/sh: nodemon: not found
error Command failed with exit code 127.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Nodemon is not installed globally in the host machine. Adding RUN yarn add global nodemon on Dockerfile just adds an extra warning ../package.json: No license field in the container error.

I currently have the following package.json file with the following dev dependencies

# package.json
...
"scripts": {
    "build": "tsc -p .",
    "start": "node ./dist/src/index.js",
    "dev": "nodemon",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "@types/express": "^4.17.11",
    "@types/node": "^14.14.34",
    "nodemon": "^2.0.7",
    "ts-node": "^9.1.1",
    "typescript": "^4.2.3"
  },
...

and nodemon.json as

{
    "watch": [
      "src"
    ],
    "ext": "ts,json",
    "ignore": [
        "src/**/*.spec.ts"
    ],
    "exec": "yarn ts-node src/index.ts"
  }
1 Answers

Try with below,

#Dockerfile

FROM node:alpine

WORKDIR /backend

COPY package*.json .

RUN yarn

RUN yarn add global nodemon

COPY . .

EXPOSE 5000

CMD ["yarn", "dev"]

SIDE NOTE: You better not using nodemon in production. Serve build files as in your start script. You have to add the build command before yarn start.

#Dockerfile

FROM node:alpine

WORKDIR /backend

COPY package*.json .

RUN yarn

COPY . .

RUN yarn run build

EXPOSE 5000

CMD ["yarn", "start"]
Related