Node.js doesn't natively support ES6 imports. It uses CommonJS module system. However, Babel enables the use of ES6 modules in node. Babel basically transpiles all source files to ES5 and CommonJS so that it works natively.
npm install @babel/core @babel/node --save-dev
npm install @babel/preset-env --save-dev
{
"presets": [
"@babel/preset-env"
]
}
"scripts": {
"anyTaskName": "nodemon --exec babel-node src/mainModule.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
Using the terminal, type the commmand to run the npm task added ('anyTaskName') to the 'scripts' in last step.
npm run anyTaskName
npm install @babel/core @babel/cli --save-dev
"scripts": {
"build": "babel src --out-dir out --source-maps"
}
We can use preLaunchTask
to transpile es6 before running
{
"type": "node",
"request": "launch",
"name": "Debug from Source Maps",
"program": "${workspaceFolder}/out/index.js",
"preLaunchTask": "npm: build",
}