创建 ts

编程入门 行业动态 更新时间:2024-09-28 01:22:12

创建 <a href=https://www.elefans.com/category/jswz/34/1770750.html style=ts"/>

创建 ts

我正在尝试创建一个准系统存储库,使用 ts-node 和 ESM 设置项目。这个问题已被问到,但有几个过时的答案 - 甚至最近的答案似乎也存在诸如here

之类的问题

我创建了一个

src/index.ts
文件,我正在尝试运行:

src/index.ts

console.log('hi');

这是我的

package.json

{
  "name": "coolProjName",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "compile": "tsc",
    "dev": "ts-node --esm src/index.ts"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^20.4.2",
    "ts-node": "^10.9.1",
    "typescript": "^5.1.6"
  }
}

还有我的 tsconfigs:

tsconfig.node20.json

{
  "$schema": "",
  "display": "Node 20",
  "_version": "20.1.0",

  "compilerOptions": {
    "lib": ["ES2022"],
    "module": "Node16",
    "target": "es2022",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node16"
  }
}

tsconfig.json

{
    "extends": "./tsconfig.node20.json",
    "compilerOptions": {
      "module": "esnext",
      "outDir": "./dist",
    },
    "include": ["./src/**/*"]
}

我已经遵循了我能找到的所有建议,但是

npm run dev
结果是:

类型错误 [ERR_UNKNOWN_FILE_EXTENSION]:未知的文件扩展名“.ts”

对于我的index.ts 文件。我该如何解决这个问题?

节点版本 - 20.4.0

回答如下:

从 package.json 中删除“type”:“module”

如果您不想删除“type”:“module”(例如,如果您在.ts中使用允许从模块推断类型的导入语句),那么您可以在tsconfig中使用以下选项。 json:

{
 "compilerOptions": {
   "esModuleInterop": true,
 }
}

来源

更多推荐

创建 ts

本文发布于:2024-05-31 07:11:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!