Firstly, node
can’t run typescript
files directly. You need to compile the typescript
files to javascript
files and then run them with node
.
The following command won’t work!
1 | node ./src/main.ts |
You’ll got error like this:
1 | (node:13596) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. |
How to solve this problem?
- Use
ts-node
to runtypescript
files directly.npm install -g ts-node
ts-node ./src/main.ts
- Use
tsc
to compiletypescript
files tojavascript
files and then run them withnode
.npm install -g typescript
tsc ./src/main.ts
node ./src/main.js
Happy coding!