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
发布评论