myesn

myEsn2E9

hi
github

Node.js: v18.x 快速集成 TypeScript + ESM + 最嚴格

  1. mkdir tests && cd tests
  2. yarn init -y
  3. yarn global add typescript ts-node
  4. yarn add -D @tsconfig/node18-strictest-esm typescript ts-node tslib @types/node concurrently nodemon
    Reference
  5. 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/**/*"
      ]
    }
    
  6. 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.

Reference: https://www.cnblogs.com/myesn/p/15739730.html

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。