title: Nestjs docker run Error: Cannot find module '/usr/src/app/dist/main'
body: <p>I built nest app and apply docker for deploy to server
  Nest app work as well in local dev environment
  I wrote <code>Dockerfile</code> by refer docker tutorial video/article</p>
  <p>Dockerfile code below :</p>
  <pre><code># Dockerfile
  From node:16
  # Specify app directory
  WORKDIR /usr/src/app
  
  # Copy package.json and package-lock.json
  COPY package*.json ./
  
  # Install dependencies
  RUN npm install
  
  # Paste source
  COPY . .
  
  # Build
  RUN npm run build
  
  # Set port
  EXPOSE 3000
  
  # Run app
  CMD [ &quot;node&quot;, &quot;dist/main&quot;]
  # CMD [ &quot;npm&quot;, &quot;run&quot;, &quot;start:prod&quot;] # for testing
  </code></pre>
  <p>I guess scripts has not changed but write it down in case you don't know.</p>
  <pre><code># package.json (scripts)
  &quot;scripts&quot;: {
      &quot;prebuild&quot;: &quot;rimraf dist&quot;,
      &quot;build&quot;: &quot;nest build&quot;,
      &quot;format&quot;: &quot;prettier --write \&quot;src/**/*.ts\&quot; \&quot;test/**/*.ts\&quot;&quot;,
      &quot;start&quot;: &quot;nest start&quot;,
      &quot;start:dev&quot;: &quot;nest start --watch&quot;,
      &quot;start:debug&quot;: &quot;nest start --debug --watch&quot;,
      &quot;start:prod&quot;: &quot;node dist/main&quot;,
      &quot;lint&quot;: &quot;eslint \&quot;{src,apps,libs,test}/**/*.ts\&quot; --fix&quot;,
      &quot;test&quot;: &quot;jest&quot;,
      &quot;test:watch&quot;: &quot;jest --watch&quot;,
      &quot;test:cov&quot;: &quot;jest --coverage&quot;,
      &quot;test:debug&quot;: &quot;node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand&quot;,
      &quot;test:e2e&quot;: &quot;jest --config ./test/jest-e2e.json&quot;,
      &quot;typeorm&quot;: &quot;ts-node ./node_modules/typeorm/cli.js&quot;
    },
  </code></pre>
  <p>I understand docker build process the above comments in Dockerfile and write like that
  And run <code>docker build ./ -t &lt;name&gt;</code>, <code>docker run &lt;name&gt;</code>
  But error occurred docker build and <strong>run</strong> that <code>Error: Cannot find module '/usr/src/app/main'</code></p>
  <p>Error message below:</p>
  <pre><code>% docker run &lt;name&gt;
  node:internal/modules/cjs/loader:936
    throw err;
    ^
  
  Error: Cannot find module '/usr/src/app/dist/main'
      at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
      at Function.Module._load (node:internal/modules/cjs/loader:778:27)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
      at node:internal/main/run_main_module:17:47 {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
  }
  </code></pre>
  <p>My question/curious is</p>
  <ol>
  <li>Why <code>WORKDIR</code> command has not create <code>/usr/src/app</code> directory</li>
  <li>Why <code>RUN npm run build</code> has not create bundled file(main.js) in dist directory (I confirmed run <code>npm run build</code> command directly in dev environment, that create <code>main.js</code> in dist directory)</li>
  </ol>
  <p>And I tried</p>
  <ol>
  <li>Change WORKDIR <code>&lt;path&gt;</code> (access, permissions ..)</li>
  <li>Remove <code>node_module</code>, <code>package-lock.json</code> and <code>npm install</code> (re-install dependencies)</li>
  <li>Comment out <code>RUN npm run build</code>, <code>CMD [&quot;node&quot;, &quot;dist/main&quot;]</code> and add command `CMD [&quot;npm&quot;, &quot;run&quot;, &quot;start:prod&quot;]</li>
  <li>And parameter changes ..</li>
  </ol>
  <p>Please advice to me
  thanks</p>