Kenny Kenny - 1 year ago 76
Javascript Question

what's needed to setup in order to run ES6 `import ... from ...` in a node project?

I am reading a source code of

, in order to understand the source code, I changed it into the following code with
import ... from ...
for running:

import rgb from "./rgb";
import array from "./array";
import date from "./date";
import number from "./number";
import object from "./object";
import string from "./string";
import constant from "./constant";

var interpolateValue = function(a, b) {
// set var t and c
var t = typeof b, c;

// if b is null or t is type boolean,
return b == null || t === "boolean" ? constant(b)
: (t === "number" ? number
: t === "string" ? ((c = color(b)) ? (b = c, rgb) : string)
: b instanceof color ? rgb
: b instanceof Date ? date
: Array.isArray(b) ? array
: isNaN(b) ? object
// (a,b) is 2 function args
: number)(a, b);

console.log(interpolateValue("foo", "bar")(0.5));

I tried to run this code inside a node project with
as dependencies. However, I recieved the following error pointing at
in console:

SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3

my question:

What did I do wrong? Do I need Babel to work with node? If so, I did installed Babel with
npm install --save-dev babel-cli
and added
"build": "babel src -d lib"
, but still got the same error above. Maybe I didn't do it right. Could anyone help?

Thank you!

Answer Source

Yes you need babel. Have you checked out the documentation here:

$ npm install --save-dev babel-core


require("babel-core").transform("code", options);

after that make sure you've setup your .babelrc file:

  "presets": ["es2015"]

Hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download