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-nodeto runtypescriptfiles directly.npm install -g ts-nodets-node ./src/main.ts
- Use
tscto compiletypescriptfiles tojavascriptfiles and then run them withnode.npm install -g typescripttsc ./src/main.tsnode ./src/main.js
Happy coding!