Error: Cannot find module ‘/next’, docker, next.js

I want to create containers out of my next.js application however I encounter a weird error that I couldn’t solve after about 3 days of investigating.


  "name": "client",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "next",
  "keywords": [],
  "author": "Shahin Ghasemi",
  "license": "ISC",
  "dependencies": {
    "next": "^9.4.4",
    "react": "^16.13.1",
    "react-dom": "^16.13.1"


FROM node:alpine
COPY package.json .
RUN npm install
CMD [ "next", "run","dev" ]

When I run the command docker build -t x/y:tag . it creates the image successfully. however, when I want to run the container it gives me this error:

$ docker run  abbe27e7cc4d
  throw err;
Error: Cannot find module '/next'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1020:15)
at Function.Module._load (internal/modules/cjs/loader.js:890:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
  requireStack: []

Source: StackOverflow