[英]"SyntaxError: Cannot use import statement outside a module" but tsconfig.json `module` already `commonjs`
I am somewhat new to typescript, and I am working in a fork of someone else's existing repository, trying to run tests against it.我对 typescript 有点陌生,我正在其他人的现有存储库的一个分支中工作,试图针对它运行测试。
Summary概括
I have written a test script examples/test.ts
and am attempting to debug it using the vscode debugger.我已经编写了一个测试脚本
examples/test.ts
并尝试使用vscode 调试器对其进行调试。 When I run the script with ts-node./examples/test.ts
, the script executes successfully.当我使用
ts-node./examples/test.ts
运行脚本时,脚本会成功执行。 However, when I try to run it using the vscode debugger, I get the famous SyntaxError: Cannot use import statement outside a module
.但是,当我尝试使用 vscode 调试器运行它时,我得到了著名的
SyntaxError: Cannot use import statement outside a module
。 This error occurs on line 1 of my typescript test, where I first try to import {Foo} from '../dist'
, where Foo
is an object under test.此错误发生在我的 typescript 测试的第 1 行,我首先尝试
import {Foo} from '../dist'
,其中Foo
是正在测试的 object。
Attempted Fixes尝试修复
I've been researching for a little while, the most common fix for this seems to be to set module
to commonjs
in the tsconfig.json
compilerOptions
.我已经研究了一段时间, 最常见的解决方法似乎是在
tsconfig.json
compilerOptions
中将module
设置为commonjs
。 But, this project already has that set up correctly.但是,这个项目已经正确设置了。
Another common fix is to set "type":"module"
in package.json
, but when I do that I cause new errors in the vscode debugger. 另一个常见的修复方法是在
package.json
中设置"type":"module"
,但是当我这样做时,我会在 vscode 调试器中导致新的错误。 Specifically, Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /path/truncated/by/me/examples/test.ts
.具体来说,
Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /path/truncated/by/me/examples/test.ts
。 The fact that I can run the script normally outside the debugger makes me think it's more likely to be tsconfig.json
or similar configuration, of course I may be wrong.我可以在调试器之外正常运行脚本的事实让我认为它更有可能是
tsconfig.json
或类似配置,当然我可能是错误的。
Relevant config files:相关配置文件:
.vscode/launch.json .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/examples/test.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
tsconfig.json tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es2015", "dom"],
"allowJs": true,
"skipLibCheck": true,
"sourceMap": true,
"outDir": "dist",
"strict": true,
"noImplicitAny": true,
"moduleResolution": "node",
"baseUrl": "./",
"esModuleInterop": true
},
"include": ["*"],
"exclude": ["node_modules"]
}
Versions版本
Running VSCode 1.52.1 on up-to-date macOS.在最新的 macOS 上运行 VSCode 1.52.1。
ts-node --version
gives v9.1.1
, and the project uses node --version
of v12.14.1
. ts-node --version
给出v9.1.1
,项目使用node --version
of v12.14.1
。
I had the exact problem.我有确切的问题。 Here's what finally worked for my cli project (using vscode 1.61.2).
这是我的 cli 项目最终奏效的方法(使用 vscode 1.61.2)。
launch.json:发射.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program",
"program": "${workspaceFolder}/src/cli.ts",
"request": "launch",
"preLaunchTask": "npm: build",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
}
]
}
tsconfig.json: tsconfig.json:
{
"compilerOptions": {
"target": "ES2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "commonjs" /* Specify what module code is generated. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"outDir": "./lib",
"importHelpers": false,
"sourceMap": true
}
}
package.json scripts section: package.json 脚本部分:
"scripts": {
"start": "node lib/cli.js",
"build": "tsc"
},
package.json devDependencies package.json devDependencies
"devDependencies": {
"@types/node": "^16.11.6",
"typescript": "^4.4.4"
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.