How to use `import` statement


#1

How do I use an import statement? For example, import fs from 'fs'. I see it is supported in https://glitch.com/edit/#!/babel-latest, but I don’t understand what enables it.

What causes the Logs Error in the screenshot below to be suppressed.

Thanks in advance :slight_smile:


#2

My understanding is that Node.js (even the latest version, 7.7.4) doesn’t yet support import. So you have to either use Babel, as in the example project you linked to, or use the old style of importing libraries with require.

Most ES6 (aka ECMAScript 2015) features work in the latest versions of Node.js but, for some technical reasons, the new syntax for importing modules is lagging.


#3

Hi Brian,

as Daniel said, you need Babel to use the import statements in Node. If you look at package.json in the project you linked, you will notice that the dependencies include babel, and that the start script uses a modified version of node, babel-node, instead of the vanilla node binary to run server.js.


#4

Thanks @dreeves and @etamponi. I used babel-node and that fixed the issue.