Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile typescript code in dockerfile does not work (Cannot find name 'process')

I have the following Dockerfile:

FROM node:latest
WORKDIR /usr/src/app

ENV NODE_ENV=production

COPY package*.json .
    
RUN npm install && npm i -g typescript

COPY . .

RUN  tsc

CMD [ "node", "./dist/index.js" ]

And the following package.json:

  "dependencies": {
    "discord.js": "^12.5.3",
    "moment": "^2.29.1"
  },
  "devDependencies": {
    "@types/node": "^14.14.37",
    "typescript": "^4.2.4"
  }

When I try to build the image, I get the following error:

Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`

Does anyone know why that happens? I have @types/node installed.

like image 852
MauriceNino Avatar asked Jun 10 '26 01:06

MauriceNino


1 Answers

devDependencies won't be installed for production mode.

So, you can:

  • Move these packages to dependencies or
  • Remove NODE_ENV environment variable or
  • Use multi-stage build, where you will build TypeScript on the first stage (keep dependencies as is, but you don't need production here), and use JS files on the second stage (you don't need devDependencies here, so, production will work).
like image 85
Exploding Kitten Avatar answered Jun 13 '26 04:06

Exploding Kitten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!