1

I have a Node.JS project that uses only ES6 modules. The package.json contains "type": "module" and all files only use ES6 import.

I want to debug the project. I have been searching on the internet and it seems everyone is using Babel to transpile the code for some reason. I just want to run the files directly. It worked in Visual Studio 2017, but it started freezing at the start which is why I tried VS Code.

I've put this in my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "args": [
                "--experimental-modules"
            ],
            "program": "${workspaceFolder}\\server\\neuralGenerations.js"
        }
    ]
}

But when I launch, I get this error:

D:\Programs\NodeJS\node.exe .\server\neuralGenerations.js --experimental-modules
Debugger attached.
Waiting for the debugger to disconnect...
internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\programming\SmartySnek\SmartySnek\server\neuralGenerations.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_REQUIRE_ESM'
}

If I just go to the server folder and do node --experimental-modules neuralGenerations.js it runs just fine, but I need to debug it.

How to directly run the ES6 file from VS Code?

I have node v12.16.1 and VSCode v1.56.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778

1 Answers1

0

Try this configuration attribute in launch.json:

"configurations": [
  {
    "name": "run module",
    "type": "node",
    "request": "launch",
    "args": [
      "--experimental-modules",
      "--experimental-json-modules",
      "${workspaceFolder}/index.js"
    ]
  }
]

${workspaceFolder}/index.js is the path of your startup script.

--experimental-modules and --experimental-json-modules will sign node to run in ES6 mode while debugging.

Good luck!

Tyler2P
  • 2,324
  • 26
  • 22
  • 31