#StackBounty: #node.js #ecmascript-6 #import #babeljs How to configure .babelrc to support ES6 module imports and async/await?

Bounty: 50

Desired Behaviour

I am trying to import code from one file into another with:

lib.js

// generate unique id
export const guid = () => {
    const s4 = () => {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
        s4() + '-' + s4() + s4() + s4();
}

// get current date as ISO string  
export const currentDateTimeISOString = () => {
    var iso_string = new Date().toISOString();
    return iso_string;
}

// convert boolean string to boolean
export const stringToBoolean = (val) => {
    var a = {
        'true': true,
        'false': false
    };
    return a[val];
}

app_es6.js

import { guid, currentDateTimeISOString, stringToBoolean } from './src/js/lib';  

Actual Behaviour

After build I get the error:

export const guid = () => {
^^^^^^

SyntaxError: Unexpected token export

What I’ve Tried

I’ve googled this error and come across various solutions.

The most up to date approach seems to be:

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

source

I currently have the following babel related dev dependencies in package.json:

"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-0": "^6.24.1",

And .babelrc is:

{
    "presets": [
        [
            "env",
            {
                "targets":
                {
                    "node": "current"
                }
            }
        ]
    ]
}

I recently changed .babelrc to the above in order to handle async/await usage, it used to be:

{
    "presets": [
        "env",
        "stage-0"
    ]
}

My build script in package.json is:

"build-server-file": "babel app_es6.js --out-file app.js",

I’m concerned about implementing a solution that is outdated or breaks functionality with another part of the codebase (ie, if i revert to my previous version of .babelrc then async/await will throw errors). I’ve also read that stage-x is depreciated.

Question

What is the most up to date way to import/export modules in ES6 in a Node.js environment whilst still supporting the .babelrc requirements for async/await?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.