【问题标题】:"SyntaxError: Cannot use import statement outside a module" but tsconfig.json `module` already `commonjs`“语法错误:不能在模块外使用导入语句”但 tsconfig.json `module` 已经是 `commonjs`
【发布时间】:2021-11-04 12:23:26
【问题描述】:

我对 typescript 有点陌生,我在别人的现有存储库的一个分支中工作,试图针对它运行测试。

总结

我已经编写了一个测试脚本examples/test.ts 并尝试使用 vscode 调试器对其进行调试。当我使用ts-node ./examples/test.ts 运行脚本时,脚本执行成功。然而,当我尝试使用 vscode 调试器运行它时,我得到了著名的SyntaxError: Cannot use import statement outside a module。此错误发生在我的 typescript 测试的第 1 行,我首先尝试import {Foo} from '../dist',其中Foo 是被测对象。

尝试修复

我已经研究了一段时间,the most common fix 因为这似乎是在tsconfig.json compilerOptions 中将module 设置为commonjs。但是,这个项目已经正确设置了。

Another common fix 是在package.json 中设置"type":"module",但是当我这样做时,我会在 vscode 调试器中导致新的错误。具体来说,Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /path/truncated/by/me/examples/test.ts。可以在调试器之外正常运行脚本的事实让我觉得更有可能是tsconfig.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

{
"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"]
}

版本

在最新的 macOS 上运行 VSCode 1.52.1。 ts-node --version 给出v9.1.1,项目使用node --versionv12.14.1

【问题讨论】:

    标签: node.js typescript visual-studio-code vscode-debugger tsconfig


    【解决方案1】:

    我遇到了确切的问题。这是我的 cli 项目最终奏效的方法 (使用 vscode 1.61.2)。

    launch.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:

    {
      "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": {
        "start": "node lib/cli.js",
        "build": "tsc"
      },
    

    package.json devDependencies

      "devDependencies": {
        "@types/node": "^16.11.6",
        "typescript": "^4.4.4"
      },
    

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 2021-12-07
      • 1970-01-01
      • 2021-08-12
      • 2021-07-02
      • 1970-01-01
      • 2020-05-09
      • 2021-10-31
      • 2021-01-05
      相关资源
      最近更新 更多