Node.js
-
Create a new npm project
-
Create a new directory and cd into it
-
Initialize a new npm project
npm init
-
-
Install
ts-nodeandtypescriptnpm i ts-node typescript -
Create
tsconfig.jsonnpx --package=typescript tsc --init-
If you forget this step, you may get weird TypeScript errors, such as
Cannot find name 'require'.
-
-
Update
package.json-
Update references to files with
.jsextensions, e.g."main": "app.ts"(Even if this is a fresh TypeScript project, you may have references to
.jsfiles depending on how you usednpm init) -
Update anything in
scriptsusingnodeto usets-node, e.g.From:
"start": "node ."To:
"start": "ts-node ."
-
Troubleshooting
Cannot find module '...' or its corresponding type declarations.
If you’re using TypeScript with Node.js:
-
Make sure you have the Node types package installed:
npm i --save-dev @types/node -
If you still get the error and you don’t have a
tsconfig.jsonfile, generate one:npx -p typescript tsc --init