How to use `import` statement

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:

1 Like

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.

2 Likes

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.

2 Likes

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

3 Likes

Unfortunately the initial project does not exist anymore.

How do you use the ‘import’ method in 2019?

2 Likes

Hello @pi43r,

The project can now be found here! => https://glitch.com/edit/#!/babel

2 Likes

Hi @Callum-OKane ! I’m getting a 504 timeout error on the project that you linked to, and it seems from the comments (in server.js) that the original user was still encountering errors. Do you know if that’s a new development (i.e. was it working when you linked to it?) I’m also trying to get import calls working and am not sure if I’m implementing it wrong, or if I’m missing something specific to glitch. (And thank you for linking to it – it’s still been helpful even though I’m still on the trail :footprints: !)