简体   繁体   English

VSC 调试器抛出语法错误:无法在模块外使用导入语句

[英]VSC Debugger throwing SyntaxError: Cannot use import statement outside a module

Prior to some recent update of Visual Studio Code I was able to debug my Node.js Apps written in TypeScript with this launch.json configuration在最近更新 Visual Studio Code 之前,我能够使用此launch.json配置调试用 TypeScript 编写的 Node.js 应用程序

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug API",
            "protocol": "inspector",
            "program": "${workspaceFolder}/apps/api/src/main.ts",
            "outFiles": ["${workspaceFolder}/dist/apps/api/main.js"],
            "sourceMaps": true
        }
    ]
}

But now I'm getting this error但是现在我收到了这个错误

import * as express from 'express';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1054:16)
    at Module._compile (internal/modules/cjs/loader.js:1102:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
Process exited with code 1

Any idea of how can I fix this?知道如何解决这个问题吗? Or at least make it work while VSC Team provides an answer on their GitHub https://github.com/microsoft/vscode/issues/102834或者至少让它工作,同时 VSC 团队在他们的 GitHub https://github.com/microsoft/vscode/issues/102834上提供答案

You can do the following:您可以执行以下操作:

  1. Add (if not already exist) a build step in the package.json , something like this;package.json中添加(如果尚不存在)构建步骤,如下所示;

     "ci-build": "npx tsc",
  2. I have in the tsconfig.json the following options;我在tsconfig.json中有以下选项;

     "compilerOptions": { "outDir": "dist", "rootDir": "src", "module": "commonjs" "sourceMap": true, "lib": ["esnext", "dom"], "strict": true, "esModuleInterop": true, "target": "es2017" }

    Note : Make sure to set the sourceMap to true注意:确保将sourceMap设置为true

  3. My launch.json looks like this;我的launch.json看起来像这样;

     { "type": "node", "request": "launch", "name": "Build Project", "program": "${workspaceFolder}/src/index.ts", "preLaunchTask": "npm: ci-build", "sourceMaps": true, "smartStep": true, "internalConsoleOptions": "openOnSessionStart", "outFiles": ["${workspaceFolder}/dist/**/*.js"] }

For further reading, see my blog post on the topic如需进一步阅读,请参阅我关于该主题的博客文章

According to a comment on this github issue , VSC Team has provided a fix for version 1.48 of Visual Studio Code:根据对这个 github 问题的评论,VSC 团队已经为 Visual Studio Code 1.48 版本提供了修复:

{
    "type": "node",
    "request": "launch",
    "name": "Debug launcher",
    "protocol": "inspector",
    "program": "${workspaceFolder}/apps/launcher/src/main.ts",
    "outFiles": ["${workspaceFolder}/dist/**/*.js"],
    "sourceMaps": true
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使在 package.json 中指定 "type":"module" 用于导入外部文件,仍然抛出 SyntaxError: Cannot use import statement outside a module - Even specifing "type":"module" in package.json for importing external file, still throwing SyntaxError: Cannot use import statement outside a module “语法错误:不能在模块外使用导入语句”但 tsconfig.json `module` 已经 `commonjs` - "SyntaxError: Cannot use import statement outside a module" but tsconfig.json `module` already `commonjs` 尝试在 VSCode 上调试 TypeScript 程序时出现“语法错误:无法在模块外使用导入语句” - “SyntaxError: Cannot use import statement outside a module” while trying to debug a TypeScript program on VSCode 如何使用部署在 Azure 应用服务上的 Vue CLI 项目解决“SyntaxError: Cannot use import statement outside a module” - How to solve "SyntaxError: Cannot use import statement outside a module" with Vue CLI project deployed on Azure App Service 语法错误:不能在模块外使用 import 语句 - Syntax error: Cannot use import statement outside a module VSC 说模块已安装但不会在代码中导入 - VSC says module is installed but won't import it in code vscode调试器找不到模块 - vscode Debugger Cannot find module 如何在 VSC 中使用来自 pip3 的电话号码模块? - How do I use phonenumber module from pip3 in VSC? 我无法在 Windows 10 中使用 pipenv 和 bash 在 VSC 中使用 pylint - I cannot use pylint in VSC using pipenv & bash for windows 10 有没有办法检测调试器是否创建了 VSC 窗口 - Is there a way of detecting if a VSC window is created by the debugger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM