0

I just started learning Docker and i'm trying to run my node ts app with few simple commands. Already googled and tried a lot of things but still no success. App works perfectly when i run it directly from terminal, so mistake might only be in the Dockerfile, nothing else crossed my mind. It always builds successfully, but breaks when i try to run. Dockerfile Output

FROM node:14.17.1-alpine
# Install base packages
RUN apk update
RUN apk upgrade
# Change TimeZone
RUN apk add --update tzdata
ENV TZ Europe/Berlin
# Clean APK cache
RUN rm -rf /var/cache/apk/*
WORKDIR /app
COPY package*.json ./
RUN npm i
COPY . .
ENV NODE_ENV=production
CMD ["node","src/app.ts"]

Any help would be really appreciated, Thanks

Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35

1 Answers1

0

Typscript app cannot be run with Node. You need to compile .ts files to .js files and then you can run .js file with Node.

For more detailed you can checkout a blogpost on how to containerize. For example: https://itnext.io/dockerize-a-typescript-app-in-15-mins-a0e8c1e904b3

But there is ts-node, REPL kind of thing for TS. You can add it in the package.json as devDependencies and then can do:

CMD ["npx", "ts-node", "src/app.ts"]

You can check more here: How to run TypeScript files from command line?

EDIT:
if you have tsconfig file then you can install tsc and run it in the Dockerfile to compile. Then you can run the compiled JS file in Node.

You can take hint from this: How to compile typescript in Dockerfile

RUN npm install tsc -g
RUN tsc
CMD ["node", "<urapppath>/app.js"]
Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35
  • Now there's different error, not seeing imports ... Cannot find module in a few places :/ Those are imports from the different repository, why would that even be a problem. – Stefan Stanković Jul 07 '21 at 08:34
  • what method are u following? tsc or ts-node? also do the same thing locally run all the commands locally.. maybe remove node_modules folder to get a sense of what is the problem. but probably it's better to close this question and create another question. – Aritra Chakraborty Jul 07 '21 at 08:43