Node.js
-
Create a new npm project
-
Create a new directory and cd into it
-
Initialize a new npm project
npm init
-
-
Install
ts-node
andtypescript
npm i ts-node typescript
-
Create
tsconfig.json
npx --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
.js
extensions, e.g."main": "app.ts"
(Even if this is a fresh TypeScript project, you may have references to
.js
files depending on how you usednpm init
) -
Update anything in
scripts
usingnode
to 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.json
file, generate one:npx -p typescript tsc --init