I'm trying to get the hang of es6 imports in node and am trying to use the syntax provided in this example:
Cheatsheet Link: https://hackernoon.com/import-export-default-require-commandjs-javascript-nodejs-es6-vs-cheatsheet-different-tutorial-example-5a321738b50f
I'm looking through the support table: http://node.green/, but was not able to find what version supports the new import statements (I tried looking for the text import/require) I'm currently running node 8.1.2 and also believe that since the cheatsheet is referring to .js files it should work with .js files.
As I run the code (taken from cheatsheet's 1st example):
import { square, diag } from 'lib';
I get the error: SyntaxError: Unexpected token import.
Reference to lib I'm trying to import:
//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}
What am I missing and how can I get node to recognize my import statement?
solution
https://www.npmjs.com/package/babel-register
below is definition of mixins/index.js
that worked just fine inside my node.js CLI app.
Wasted about 3 hours.
I just wanted to use the
import
andexport
in js files.Everyone says not possible. But, as of May 2018, it's possible to use above in plain node.js, without any modules like babel, etc.
Here is a simple way to do it.
Create below files, run and see output for yourself.
Also don't forget to see
Explanation
below.myfile.mjs
index.mjs
run
output
Explanation:
Hope that helped.
.babelrc
entry point node.js app
Link How To Enable ES6 Imports in Node.JS https://timonweb.com/posts/how-to-enable-es6-imports-in-nodejs/
If you are using the modules system on the server side, you do not need to use Babel at all. To use modules in NodeJS ensure that:
That's it.
However and this is a big however, while your shinny pure ES6 code will run in an environment like NodeJS (at writing 9.5.0) you will still have the craziness of transpilling just to test. Also bear in mind that Ecma has stated that release cycles for Javascript are going to be faster, with newer features delivered on a more regular basis. Whilst this will be no problems for single environments like NodeJS, its a slightly different proposition for browser environments. What is clear is that testing frameworks have a lot to do in catching up. You will still need to probably transpile for testing frameworks. I'd suggest using jest.
Also be aware of bundling frameworks, you will be running into problems there
Back to Jonathan002's original question about
based on the article by Dr. Axel Rauschmayer, there is a plan to have it supported by default (without the experimental command line flag) in Node.js 10.x LTS. According to node.js's release plan as it is on 3/29, 2018, it's likely to become available after Apr 2018, while LTS of it will begin on October 2018.
I don't know if this will work for your case but I am running an express server with this:
This gives me the ability to import and use spread operator even though I'm only using node version 8.
You'll need to install babel-cli, babel-preset-es2015, babel-preset-stage-2 do do what I'm doing.