ts-node cannot find module typescript
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 plan to use ts-node
and nodemon
as starting point of your application, then explicitly define them in package.json
dependencies.
Then RUN npm install --production
They'll be accesible in node_modules/.bin/
~/foo/node_modules/.bin> lsis-ci@ nodetouch@ rc@ ts-node@ ts-node-transpile-only@nodemon@ nopt@ semver@ ts-node-script@ ts-script@
Then you can run ./node_modules/.bin/nodemon
or npx nodemon
as your Docker CMD
.
npx
Executes either from a local node_modules/.bin, or from a central cache, installing any packages needed in order for to run.
source: npx
Tip to improve your Dockerfile
, the right way to cache node_modules
.
COPY package*.json /usr/src/app
Note that, rather than copying the entire working directory, we are only copying the package.json file. This allows us to take advantage of cached Docker layers.
Then copy app files
COPY . /usr/src/app
Don't forget .dockerignore
node_modules
.