赞
踩
错误信息1:
E:\PersonalProject\ts-utils\test>ts-node DateChainTest.ts
(node:22636) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
E:\PersonalProject\ts-utils\test\DateChainTest.ts:1
import { DateChain } from '../src/DateChain';
^^^^^^
SyntaxError: Cannot use import statement outside a module
......
错误信息2:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for E:\PersonalProject\ts-utils\test\DateChainTest.ts
at new NodeError (node:internal/errors:371:5)
at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
at Loader.getFormat (node:internal/modules/esm/loader:105:42)
at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
at async Loader.import (node:internal/modules/esm/loader:177:17)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
at async handleMainPromise (node:internal/modules/run_main:63:12) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
ts-node
版本:^10.8.0
,typescript
版本:^4.6.3
。
tsconfig.json
文件内容如下
{
"compilerOptions": {
"baseUrl": "./",
"module": "es6",
"target": "es2020",
"allowJs": false,
"declaration": true,
"outDir": "./dist",
"strict": true
},
"exclude": [
"node_modules"
],
"include": [
"index.ts",
"src/*"
]
}
在网上查找了一些资料,出现类似问题的有很多,链接如下:
其中主要有以下几种解决方式:
方法1:
package.json
中设置"type"
属性为"module"
;tsconfig.json
中的compilerOptions
的esModuleInterop
属性值置为true
。方法2:
package.json
中移除"type": "module"
;tsconfig.json
中将compilerOptions
的module
属性改为"CommonJS"
,或者在该文件中添加"ts-node"
配置项。{
/* ... your props ... */
"ts-node": {
"compilerOptions": {
"module": "CommonJS"
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。