title: TSC not found in Docker build
body: <p>When building an image that needs to be compiled from typescript, I get this error.</p>
  <blockquote>
  <p>sh: 1: tsc: not found</p>
  </blockquote>
  <blockquote>
  <p>The command '/bin/sh -c npm run tsc' returned a non-zero code: 127</p>
  </blockquote>
  <p>Here is the relevant code:</p>
  <p>docker-compose.yaml</p>
  <pre><code>version: '3.1'
  
  services:
    nodeserver:
      build:
        context: .
        target: prod
      ports:
        - &quot;3000:3000&quot;
      volumes:
        - ./src:/app/src
        - ./public:/app/public
        - ./templates:/app/templates
  </code></pre>
  <p>Dockerfile</p>
  <pre><code>FROM node:15.11.0 AS base
  EXPOSE 3000
  ENV NODE_ENV=production
  WORKDIR /app
  COPY package*.json ./
  
  RUN npm install --only=production &amp;&amp; npm cache clean --force
  
  ##########################################################################################
  
  FROM base AS dev
  
  ENV NODE_ENV=development
  
  RUN npm install --only=development
  
  CMD npm run dev
  
  ##########################################################################################
  
  FROM dev AS source
  
  COPY dist dist
  COPY templates templates
  COPY public public
  
  RUN npm run tsc
  
  ##########################################################################################
  
  FROM base AS test
  
  COPY --from=source /app/node_modules /app/node_modules
  COPY --from=source /app/templates /app/templates
  COPY --from=source /app/public /app/public
  COPY --from=source /app/dist /app/dist
  
  CMD npm run test
  
  ##########################################################################################
  
  FROM test AS prod
  
  CMD npm start
  </code></pre>
  <p>package.json</p>
  <pre><code>{
    &quot;name&quot;: &quot;nodeserver&quot;,
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;description&quot;: &quot;&quot;,
    &quot;main&quot;: &quot;index.js&quot;,
    &quot;scripts&quot;: {
      &quot;start&quot;: &quot;node ./dist/app.js&quot;,
      &quot;deploy&quot;: &quot;git add . &amp;&amp; git commit -m Heroku &amp;&amp; git push heroku main&quot;,
      &quot;tsc&quot;: &quot;tsc --outDir ./dist&quot;,
      &quot;dev&quot;: &quot;npm run ts-watch&quot;,
      &quot;test&quot;: &quot;npm run jest --runInBand&quot;,
      &quot;ts-watch&quot;: &quot;tsc-watch --project . --outDir ./dist --onSuccess \&quot;nodemon ./dist/app.js\&quot;&quot;
    },
    &quot;jest&quot;: {
      &quot;testEnvironment&quot;: &quot;node&quot;
    },
    &quot;repository&quot;: {
      &quot;type&quot;: &quot;git&quot;,
      &quot;url&quot;: &quot;git+https://github.com/MiquelPiza/nodeserver.git&quot;
    },
    &quot;author&quot;: &quot;&quot;,
    &quot;license&quot;: &quot;ISC&quot;,
    &quot;bugs&quot;: {
      &quot;url&quot;: &quot;https://github.com/MiquelPiza/nodeserver/issues&quot;
    },
    &quot;homepage&quot;: &quot;https://github.com/MiquelPiza/nodeserver#readme&quot;,
    &quot;dependencies&quot;: {
      &quot;@sendgrid/mail&quot;: &quot;^7.4.2&quot;,
      &quot;bcryptjs&quot;: &quot;^2.4.3&quot;,
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;handlebars&quot;: &quot;^4.7.7&quot;,
      &quot;jsonwebtoken&quot;: &quot;^8.5.1&quot;,
      &quot;lodash&quot;: &quot;^4.17.20&quot;,
      &quot;mongodb&quot;: &quot;^3.6.4&quot;,
      &quot;mongoose&quot;: &quot;^5.11.19&quot;,
      &quot;multer&quot;: &quot;^1.4.2&quot;,
      &quot;socket.io&quot;: &quot;^4.0.0&quot;,
      &quot;validator&quot;: &quot;^13.5.2&quot;
    },
    &quot;devDependencies&quot;: {
      &quot;@types/bcryptjs&quot;: &quot;^2.4.2&quot;,
      &quot;@types/express&quot;: &quot;^4.17.11&quot;,
      &quot;@types/jsonwebtoken&quot;: &quot;^8.5.0&quot;,
      &quot;@types/lodash&quot;: &quot;^4.14.168&quot;,
      &quot;@types/mongoose&quot;: &quot;^5.10.3&quot;,
      &quot;@types/multer&quot;: &quot;^1.4.5&quot;,
      &quot;@types/node&quot;: &quot;^14.14.33&quot;,
      &quot;@types/sendgrid&quot;: &quot;^4.3.0&quot;,
      &quot;@types/validator&quot;: &quot;^13.1.3&quot;,
      &quot;env-cmd&quot;: &quot;^10.1.0&quot;,
      &quot;jest&quot;: &quot;^26.6.3&quot;,
      &quot;nodemon&quot;: &quot;^2.0.7&quot;,
      &quot;supertest&quot;: &quot;^6.1.3&quot;,
      &quot;tsc-watch&quot;: &quot;^4.2.9&quot;,
      &quot;typescript&quot;: &quot;^4.2.3&quot;
    },
    &quot;engines&quot;: {
      &quot;node&quot;: &quot;15.11.0&quot;
    }
  }
  </code></pre>
  <p>tsconfig.json</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
  
      &quot;target&quot;: &quot;es5&quot;, 
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;strict&quot;: true,
      &quot;strictNullChecks&quot;: false,
      &quot;esModuleInterop&quot;: true,
      &quot;skipLibCheck&quot;: true,
      &quot;forceConsistentCasingInFileNames&quot;: true,
    },
    &quot;include&quot;: [&quot;src&quot;]
  }
  </code></pre>
  <p>This dockerfile works:</p>
  <pre><code>FROM node:15.11.0 AS build
  
  WORKDIR /app
  COPY package.json .
  RUN npm install
  ADD . .
  RUN npm run tsc
  
  
  FROM node:15.11.0
  WORKDIR /app
  
  COPY package.json .
  RUN npm install --production
  
  ADD public ./public
  ADD templates ./templates
  COPY --from=build /app/dist dist
  
  EXPOSE 3000
  CMD npm start
  </code></pre>
  <p>I'm using this dockerfile for reference, from a Docker course: <a href="https://github.com/BretFisher/docker-mastery-for-nodejs/blob/master/typescript/Dockerfile" rel="noreferrer">https://github.com/BretFisher/docker-mastery-for-nodejs/blob/master/typescript/Dockerfile</a>
  I don't see what I'm doing wrong, the source stage should have the dev dependencies, among them typescript, so it should be able to run tsc.</p>
  <p>Any help appreciated. Thanks.</p>
  <p>EDIT:</p>
  <p>In addition to using npm ci instead of npm install, I had to copy tsconfig.json to the working directory (and copy src directory instead of dist, which is created by tsc) for tsc to work properly. This is the modified source stage in the Dockerfile:</p>
  <pre><code>FROM dev AS source
  
  COPY src src
  COPY templates templates
  COPY public public
  COPY tsconfig.json tsconfig.json
  
  RUN npm run tsc
  </code></pre>