Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "esm" in VSCode's NodeJs Debugger

I am using Vscode.

I have a js file filled with a dozen+ class definitions which I wanted to be available in another jsfile. So I used es6 imports in my other file, main.js

//main.js
import * as Helper from './helperClasses.js';
var myDoggy = new Helper.Pet("Fido");

But that wouldn't run with node, so I installed 'esm' with npm, and made a file called server.js, in which I added

//server.js
require = require("esm")(module/*, options*/)
module.exports = require("./main.js")

Now that runs with the code runner extension, or from a cmd window with the '-r esm' args (i.e. node -r esm server.js). But in the vsCode debugger, I get the following error:

import * as helper from './helperClasses.js';
       ^

SyntaxError: Unexpected token *

I tried changing the configuration settings in launch.json as follows, but that did not work:

"configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}\\game.js",
            "args": ["-r","esm","server.js"]
        }
    ]

I went and changed the .js extensions to .mjs, but then intellisense stopped working...

Is there something I'm missing? This is my first time trying to use Node and I'm just trying to easily import some helper functions.

like image 292
pleasedontcallmethat Avatar asked Aug 02 '19 05:08

pleasedontcallmethat


1 Answers

I had a problem with the accepted answer. Using runtimeArgs worked better:

    "configurations": [
        {
            "type": "node",
            "name": "import-test-files",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}/scripts/import-test-files.js",
            "runtimeArgs": ["-r", "esm"]
        }
    ]
like image 183
David Figatner Avatar answered Oct 18 '22 22:10

David Figatner