I'm deploying a dockerized typescript app on AWS. When I run the app container I get a ts-node error Error: Cannot find module 'typescript'
which is not clear to me because typescript
is defined as a devDependency.
These are my package.json
dependencies
{
"name": "app-server",
"version": "0.0.0",
"description": "description",
"author": "Marcello Bardus",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\"",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "nodemon",
"start:debug": "nodemon --config nodemon-debug.json",
"prestart:prod": "rimraf dist && tsc",
"start:prod": "node dist/main.js",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^5.4.0",
"@nestjs/core": "^5.4.0",
"@nestjs/typeorm": "^5.3.0",
"@types/mongoose": "^5.3.18",
"elliptic": "^6.4.1",
"mongoose": "^5.4.14",
"object-hash": "^1.3.1",
"pg": "^7.8.0",
"randomstring": "^1.1.5",
"reflect-metadata": "^0.1.12",
"rimraf": "^2.6.2",
"rxjs": "^6.2.2",
"sha2": "^1.0.2",
"typeorm": "^0.2.13",
"typescript": "^3.0.1"
},
"devDependencies": {
"@nestjs/testing": "^5.1.0",
"@types/express": "^4.16.0",
"@types/jest": "^23.3.1",
"@types/node": "^10.7.1",
"@types/supertest": "^2.0.5",
"jest": "^23.5.0",
"nodemon": "^1.18.10",
"prettier": "^1.14.2",
"supertest": "^3.1.0",
"ts-jest": "^23.1.3",
"ts-loader": "^4.4.2",
"ts-node": "^7.0.1",
"tsconfig-paths": "^3.5.0",
"typescript": "^3.0.1",
"tslint": "5.11.0"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
And the Dockerfile
FROM node
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install -g ts-node nodemon
RUN npm install
RUN npm install typescript
EXPOSE 8085
CMD ["nodemon", "."]
I suppose that typescript
is installed locally once the container has been built but it's not.
Thanks in advance
To solve the error "Cannot find module 'ts-node/register'", install ts-node and typescript as development dependencies by running npm install --save-dev ts-node typescript . Copied! This will add the ts-node package to the development dependencies of your project.
To solve the cannot find module 'typescript' error, make sure to install typescript globally by running the npm i -g typescript command and create a symbolic link from the globally-installed package to node_modules by running the npm link typescript command.
Tweaked version of node-dev that uses ts-node under the hood. It restarts target node process when any of required files changes (as standard node-dev ) but shares Typescript compilation process between restarts.
Finally, I got ts-node to work using npx:
Install typescript globally:
npm i typescript -g
Go to your project directory and link typescript to the project:
cd <my-project>
npm link typescript
Execute ts-node using npx:
npx ts-node <your-ts-script>.ts
I have installed typescript globally using - npm i typescript -g
Still getting the issue - Error: Cannot find module ‘@types/node/package.json‘
Then this helped me to resolve the issue - sudo npm install -D tslib @types/node
After this, I was able to run my script file using either of these -
npx ts-node <script-file>.ts
OR
ts-node <script-file>.ts
You will get detailed explanation on the official npmjs website - https://www.npmjs.com/package/ts-node
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With