Skip to content
Advertisement

Different behavior between ES6 and ES2016 using babel on cluster.on

I am trying to use cluster to exploit the benefit of having multi-core CPUs. With code:

JavaScript

node worked perfectly with output

JavaScript

However, when I tried to use import together with babel, I got problems:

JavaScript

the output (after babel) of node is:

JavaScript

This looks weird. I am using node v6.4.0, with babel 6.11.4 (babel-core 6.13.2), the content of .babelrc is:

JavaScript

Any ideas what happened?

Advertisement

Answer

Ok, I figured out the reason, ref: Difference between import X and import * as X in node.js (ES6 / Babel)?

The point is change import * as cluster from 'cluster' to import cluster from 'cluster'.

With import * as cluster from 'cluster', everything that is exportable is exported into an object with name cluster, and it has structure:

JavaScript

On the other hand, when import cluster from 'cluster', the cluster object is the default export:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement