mkdir tests && cd tests
yarn init -y
yarn global add typescript ts-node
yarn add -D @tsconfig/node18-strictest-esm typescript ts-node tslib @types/node concurrently nodemon
Reference- Create
tsconfig.json
file and add the following configuration:{ "extends": "@tsconfig/node18-strictest-esm/tsconfig.json", "compilerOptions": { "baseUrl": ".", "rootDir": "src", "outDir": "dist", "noImplicitAny": true, "removeComments": true, "sourceMap": true, "module": "CommonJS", }, "include": [ "src/**/*" ] }
- Add the following scripts to the package.json file:
"scripts": {
"dev": "ts-node-esm src/index.ts",
"build-ts": "tsc",
"build": "yarn build-ts",
"debug": "yarn build && yarn watch-debug",
"serve-debug": "nodemon --inspect dist/index.js",
"serve": "node dist/index.js",
"start": "yarn serve",
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm:watch-ts\" \"npm:serve-debug\"",
"watch-node": "nodemon dist/index.js",
"watch-ts": "tsc -w",
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm:watch-ts\" \"npm:watch-node\""
}
Note:
- ts-node is used to execute ts files directly without compilation in the development environment, such as ts-node src/index.ts
- "module": "CommonJS" in tsconfig.json is for ts-node-esm to use ES Module import correctly
- "sourceMap": true in tsconfig.json is for generating source maps, so that after executing node dist/index.js, you can debug the src/index.ts file. If you use ts-node, you don't need to configure this.