【问题标题】:"SyntaxError: Cannot use import statement outside a module" while trying to debug a TypeScript program on VSCode尝试在 VSCode 上调试 TypeScript 程序时出现“语法错误:无法在模块外使用 import 语句”
【发布时间】:2020-08-09 22:18:24
【问题描述】:

我正在使用 TypeScript 为大学做一个项目,并尝试在使用 Mocha 运行测试时在 vscode 上进行调试。我正在使用以下启动 .json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Mocha Tests",
            "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
            "args": [
                "-u",
                "tdd",
                "--timeout",
                "999999",
                "--colors",
                "${workspaceFolder}/test/*.test.ts"
            ],
            "internalConsoleOptions": "openOnSessionStart",
            "skipFiles": [
                "<node_internals>/**"
            ]
        },
    ]
}

当我运行这个配置时,我得到了错误:

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1063:16)
    at Module._compile (internal/modules/cjs/loader.js:1111:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Module.require (internal/modules/cjs/loader.js:1036:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\mocha.js:334:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\mocha.js:331:14)
    at Mocha.run (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\mocha.js:809:10)
    at Object.exports.singleRun (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\run-helpers.js:108:16)
    at exports.runMocha (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\run-helpers.js:142:13)
    at Object.exports.handler (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\run.js:292:3)
    at Object.runCommand (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\yargs\lib\command.js:242:26)
    at Object.parseArgs [as _parseArgs] (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\yargs\yargs.js:1096:28)
    at Object.parse (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\yargs\yargs.js:575:25)
    at Object.exports.main (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\lib\cli\cli.js:68:6)
    at Object.<anonymous> (c:\Users\owner\Documents\PPL\PPL-Assignment2\node_modules\mocha\bin\_mocha:10:23)
    at Module._compile (internal/modules/cjs/loader.js:1144:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

我首先尝试运行的测试称为 q2-tests.test.ts 并具有以下导入:

import fs from "fs";
import { expect } from 'chai';
import { evalL3program } from '../imp/L3-eval';
import { Value } from "../imp/L3-value";
import { Result, bind, makeOk } from "../imp/result";
import { parseL3 } from "../imp/L3-ast";

在没有调试的情况下运行时导入工作正常,当我运行npm test 时没有问题。

这似乎是导致问题的文件,但似乎不是特定于它,但通常是运行导入的问题。我尝试更改文件上的导入,但它只是在第一次导入时抛出此错误。

我对这两种工具(TypeScript\JavaScript 和 Mocha)都很陌生,而且我可能已经超出了我的深度,所以我提供的信息可能缺乏,或者可能太多,我只是不知道知道在哪里寻找问题。如果您需要更多信息,请告诉我。 另外,由于我是新手,请参考正确的位置来寻找答案并更好地理解问题,以及答案,我只是想从中学习。

【问题讨论】:

    标签: typescript visual-studio-code vscode-debugger


    【解决方案1】:

    原来我使用了错误的启动配置。这个页面有很多帮助: https://github.com/microsoft/vscode-recipes/tree/master/debugging-mocha-tests。 正确的配置是:

    {
        "type": "node",
        "request": "launch",
        "name": "Mocha All",
        "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
        "args": [
            "--timeout",
            "999999",
            "--colors",
            "--require",
            "ts-node/register",
            "test/**/*.ts",
        ],
        "console": "integratedTerminal",
        "internalConsoleOptions": "neverOpen"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      相关资源
      最近更新 更多