MarBVI
MarBVI

Reputation: 871

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

{
    // 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

Upvotes: 17

Views: 12137

Answers (3)

ArthurAkhmerov
ArthurAkhmerov

Reputation: 372

Try to add "runtimeArgs" attribute to your launch.json file:

{
"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "skipFiles": [
            "<node_internals>/**"
        ],
        "program": "${workspaceFolder}\\src\\server.ts",
        "runtimeArgs": ["-r", "ts-node/register", "-r", "tsconfig-paths/register"],
        "console": "integratedTerminal",
        "outFiles": [
            "${workspaceFolder}/**/*.js"
        ]
    }
]

}

I hope something in all of this helps you, because it helped me.

Upvotes: 0

meakgoz
meakgoz

Reputation: 588

You can do the following:

  1. Add (if not already exist) a build step in the package.json, something like this;

    "ci-build": "npx tsc",
    
  2. I have in the tsconfig.json the following options;

    "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

  3. My launch.json looks like this;

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

Upvotes: 7

MarBVI
MarBVI

Reputation: 871

According to a comment on this github issue, VSC Team has provided a fix for version 1.48 of Visual Studio Code:

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

Upvotes: 3

Related Questions