当类型设置为模块时,在`ts-node`中出现`TypeError [ERR_UNKNOWN_FILE_EXTENSION]:未知文件扩展名“.ts”`

问题描述 投票:0回答:1

我在我的express项目中配置了绝对路径,然后将类型更改为模块,因为我使用的是导入,但这给了我错误

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

这是我的

tsconfig.json

{
  "compilerOptions": {
      "module": "ESNext",
      "moduleResolution": "bundler",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "strict": true,
      "skipLibCheck": true,
      "outDir": "./dist",
      "sourceMap": true,
      "incremental": true,
      "baseUrl": ".",
      "paths": {
          "@routes/*": ["./src/routes/*"],
      },
  },
  "include": ["src/**/*"],
  "ts-node": {
    "swc": true,
    "esm": true,
    "experimentalSpecifierResolution": "node"
  },
  "exclude": ["node_modules"]
}

这是我的

package.json

{
...
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.ts",
  "type": "module",
  "scripts": {
    "build": "npx tsc",
    "start": "node ./dist/index.js",
    "dev": "nodemon --watch './src/**/*.ts' --exec 'ts-node --swc --esm' ./src/index.ts",
    "prettier": "prettier --write .",
    "prepare": "husky install"
  },
  "lint-staged": {
    "**/*": "prettier --write --ignore-unknown"
  },
...
}

我尝试使用

--esm
标志到
ts-node
但仍然存在同样的问题。我希望操作系统使绝对导入工作,但如果我删除 ts-node 并使用
concurrently \"npx tsc --watch\" \"nodemon -q ./dist/index.js\"
那么绝对导入将不起作用。我有点陷入循环了。

node.js typescript express package.json ts-node
1个回答
0
投票

运行它

node --require ts-node/register --loader ts-node/esm
© www.soinside.com 2019 - 2024. All rights reserved.