Use the ES6 import on node

If you decide to use the ES6 import instead of require, this is a simple presentation on how to achieve that using babel:

– First Install the core dependencies:

npm install babel-register babel-preset-env --save-dev

– Create a starter.js file that contain how your code has to be transpiled plus the file that contains your imports:

  presets: ["env"],

// Import the rest of our application.
module.exports = require("./test.js");

And to now your can use import on your test.js file instead of require:

import validator from "validator";

const email = "yep";

if (!validator.isEmail(email)) {
  console.log("the email is not valid");

To test the output:

node starters.js

Leave a Reply

Your email address will not be published. Required fields are marked *