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

ES6 code in npm #21

Closed
sapegin opened this issue Oct 15, 2015 · 14 comments
Closed

ES6 code in npm #21

sapegin opened this issue Oct 15, 2015 · 14 comments

Comments

@sapegin
Copy link

sapegin commented Oct 15, 2015

npm package (2.1.0) contains not transpiled ES6 code which cases an error for me:

> kao@3.0.0 mocha:base /Users/sapegin/izumi/kao
> mocha --compilers js:babel/register --require mochasetup "./test"

/Users/sapegin/izumi/kao/node_modules/fetch-mock/server.js:3
const Response = require('node-fetch').Response;
^^^^^
SyntaxError: Use of const in strict mode.
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Module._extensions..js (module.js:478:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/sapegin/izumi/kao/node_modules/babel-core/lib/api/register/node.js:214:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/sapegin/izumi/kao/mochasetup.js:6:31)
    at Module._compile (module.js:460:26)
    at normalLoader (/Users/sapegin/izumi/kao/node_modules/babel-core/lib/api/register/node.js:199:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/sapegin/izumi/kao/node_modules/babel-core/lib/api/register/node.js:216:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at /Users/sapegin/izumi/kao/node_modules/mocha/bin/_mocha:304:3
    at Array.forEach (native)
    at Object.<anonymous> (/Users/sapegin/izumi/kao/node_modules/mocha/bin/_mocha:303:10)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3
node v0.12.7
npm  v3.3.6
@wheresrhys
Copy link
Owner

Hello,

You can still use version 1.x.x of fetch-mock in node 0.12, but I recommend upgrading to node 4.2 (which is deemed stable and supported) to use fetchMock 2. There are some nice new shorthand features available as of v2.2.2, so there are benefits to using the latest code.

In node 0.12 It may be possible to create a wrapper module something like

require('babel/register');
module.exports = require('fetch-mock');

And then require this in your tests. Let me know how you get on

@sapegin
Copy link
Author

sapegin commented Oct 22, 2015

I’m already using Babel in my project but by default Babel ignores anything in node_modules.

Right now I’m using fetch-mock 1.x. I think I’ll just update Node to 4.x when I have more time at work.

@carpeliam
Copy link

@wheresrhys would upgrading to node 4.2 solve this issue when running in a Karma/Babel/PhantomJS test running scenario?

@wheresrhys
Copy link
Owner

Is this PhantomJs 2? Phantomjs 1.9.x has really out of date support for a lot of browser APIs so I wouldn't expect it to work there, but I haven't tried in Phantom2. I'll add Phantom2 and firefox to CI to see if it unearths any specific problems.

@carpeliam
Copy link

PhantomJS 2 dies because it doesn't understand ES6 syntax (the exact error is related to not understanding const).

@wheresrhys
Copy link
Owner

weird, babel should take care of that. Does your karma.conf contain a line like this https://github.com/wheresrhys/fetch-mock/blob/master/karma.conf.js#L15 and do you have babelify installed as a dev dependency?

On a different project I had a very similar problem a few days ago (unrelated to fetch-mock and running in karma-firefox), which suggests it was a babelify/karma-browserify bug. It seemed to have fixed itself spontaneously yesterday, so maybe try (if you haven't already) rm -Rf node_modules and reinstall all your dependencies too. This is the project that was broken and fixed itself https://github.com/Financial-Times/next-feature-flags-client/tree/mvt

@wheresrhys
Copy link
Owner

Yep, just added phantom and firefox to ci and all runs fine, so I reckon it's some glitchy version of something in the karma-babel-browserify setup you're running

@carpeliam
Copy link

I'm in the same situation as @sapegin, where I'm using Babel but by default Babel ignores anything in node_modules.

@wheresrhys
Copy link
Owner

I'll consider adding a babelified build to future npm releases, but have
you tried passing {ignore:false} in to babel-core/register. Does that not
work?
On Sat, 7 Nov 2015 at 5:16 p.m., Liam Morley notifications@github.com
wrote:

I'm in the same situation as @sapegin https://github.com/sapegin, where
I'm using Babel but by default Babel ignores anything in node_modules.


Reply to this email directly or view it on GitHub
#21 (comment)
.

@sapegin
Copy link
Author

sapegin commented Nov 7, 2015

That would make Babel muuuch slower.

@carpeliam
Copy link

I think this comes out of a general desire not to export library code in ES6. I think there are some tools to help with the translation, so that anyone with any setup can plop it into their node_modules without having to track down a SyntaxError. Maybe something like this? http://www.hammerlab.org/2015/07/09/bundling-and-distributing-complex-es6-libraries-in-an-es5-world/

@sapegin
Copy link
Author

sapegin commented Nov 7, 2015

For Node (not browser) it’s just Babel, like this.

@wheresrhys
Copy link
Owner

v2 of fetch-mock does explicitly drop support for non es6 environments https://github.com/wheresrhys/fetch-mock/releases/tag/v2.0.0, so I'm not too concerned about node<4 (but will document better).

Probably gonna have to include a pre-babelified version for the browser though, which is a pain. npm & babel should solve this problem

@wheresrhys
Copy link
Owner

Fixed with the PR above.

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

3 participants