Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't use es6 import #1916

Closed
luisrudge opened this issue Oct 9, 2015 · 6 comments
Closed

can't use es6 import #1916

luisrudge opened this issue Oct 9, 2015 · 6 comments

Comments

@luisrudge
Copy link

This works:
`const verticals = require('./verticals');``

This doesn't work:
import verticals from './verticals';

@luisrudge
Copy link
Author

ops. it was my bad :)

@eagor
Copy link

eagor commented Jun 1, 2016

what was the mistake?
I'm getting SyntaxError: Unexpected token import

@ScottFreeCode
Copy link
Contributor

SyntaxError: Unexpected token import means that you're running ES6 code in a pre-ES6 environment; you need to set up a compiler/transpiler. For instance, if running from the commandline, use --compilers js:<your preferred ES6 transpiler> (depending on the transpiler, you may also need other configuration to tell it to convert from ES6 to an earlier JavaScript/ES version).

@Chun-Yang
Copy link

@ScottFreeCode Thank you for your clarification. Now node 6.0 support most es6 features https://kangax.github.io/compat-table/es6/ , will mocha work with es6 by default in node 6.0 in the future? Right now it seems mocha wrap modules in some way:

> poetic-stanza@ test /Users/trent/Work/stanza
> istanbul cover _mocha

No coverage information was collected, exit without writing coverage information
/Users/trent/Work/stanza/test/tests/helpers/create-meteor-project.js:1
(function (exports, require, module, __filename, __dirname) { import path from 'path';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:511:25)
    at Module.replacementCompile (/Users/trent/Work/stanza/node_modules/append-transform/index.js:63:13)
    at Module._extensions..js (module.js:550:10)
    at Object.<anonymous> (/Users/trent/Work/stanza/node_modules/append-transform/index.js:67:4)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at /Users/trent/Work/stanza/node_modules/mocha/lib/mocha.js:220:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/trent/Work/stanza/node_modules/mocha/lib/mocha.js:217:14)
npm ERR! Test failed.  See above for more details.

@ScottFreeCode
Copy link
Contributor

@Chun-Yang As far as I can tell, Node 6 itself does not yet support the export/import (aka "harmony") syntax for modules (which doesn't appear to be listed among the features on that compatibility table, for what it's worth). Mocha isn't doing anything special to stop Node from using whatever JS/ES features it supports (I'm not even sure it could if we tried). In fact, it should even pass flags such as --harmony and --es_staging on to Node if you want to enable more of Node's supported ES6 features. I'm not sure off the top of my head which ones are supported but still require the flag, but I tried import in Node without Mocha, with and without --harmony/--es_staging/--harmony_modules, and got the error either way.

I guess that means it's not necessarily a matter of having an ES6 environment in general, but one that supports the module syntax specifically (which I thought that was part of ES6, but now I'm not sure; I'll have to look it up sometime).

codyc4321 pushed a commit to jrw4466/Kodus that referenced this issue Aug 12, 2017
@JasonJJFS
Copy link

@ScottFreeCode

This is now not strictly true, is it. If I am running Node 9.5.0 in the context of a server side app, all I have to enable is the module import system is run with the --experimental-modules flag and make sure that I serve *.js files as *.mjs. We now can run pure ES6 code with no transpiler in place, it is simply not needed. As far as I can see the release cycle of ES is also moving to a faster track, so the situation at present (2018) feels like it's now orders of magnitude harder to test pure ES6 code than to actually write and run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants