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

UglifyJs Syntax Error fails npm run build but not npm start #984

Closed
rahatarmanahmed opened this issue Oct 29, 2016 · 14 comments
Closed

UglifyJs Syntax Error fails npm run build but not npm start #984

rahatarmanahmed opened this issue Oct 29, 2016 · 14 comments

Comments

@rahatarmanahmed
Copy link

rahatarmanahmed commented Oct 29, 2016

Can you reproduce the problem with latest npm?

Yes.

Description

One of my npm modules causes the production (npm run build) builds to fail with an UglifyJs syntax error. However, I am able to run the app with npm start without any errors or problems.

Expected behavior

Should be able to build production build if development build is working.

Actual behavior

When running npm run build:

Creating an optimized production build...
Failed to compile.

static/js/main.455932b3.js from UglifyJs
SyntaxError: Unexpected token: operator (>) [./~/evolutionary/select1.js:5,0]

Environment

Run these commands in the project folder and fill in their results:

  1. npm ls react-scripts (if you haven’t ejected): [email protected]
  2. node -v: v6.9.0
  3. npm -v: v3.10.9

Then, specify:

  1. Operating system: Mac OS X Sierra
  2. Browser and version: N/A

Reproducible Demo

  1. Generate a new project with create-react-app
  2. Run npm install evolutionary
  3. Put import 'evolutionary' at the top of index.js
  4. Run npm start. Confirm that app loads in browser w/ no errors.
  5. Run npm run build. Error appears

I've never used webpack, but from my experience with browserify I would assume that ES6 syntax in dependencies would be handled just as fine as my app code.

@gaearon
Copy link
Contributor

gaearon commented Oct 29, 2016

I've never used webpack, but from my experience with browserify I would assume that ES6 syntax in dependencies would be handled just as fine as my app code.

ES6 syntax in dependencies is currently not supported because we use Uglify. This will be fixed when we switch to Babili when it's more stable.

Nevertheless it's important to stress that we aren't and won't be transpiling any dependencies. This is intentional. If your dependencies are published in ES6, your app will not work in browsers that don't support ES6.

We don't transpile dependencies because:

  • It breaks some dependencies.
  • It is extremely slow in larger projects.

This doesn't have to do with webpack per se, you can set up both browserify and webpack to transpile dependencies. But we don't recommend this and won't be doing this.

Closing as non-actionable as long as we use Uglify.

@gaearon gaearon closed this as completed Oct 29, 2016
@rahatarmanahmed
Copy link
Author

Oops, I was mistaken. You're right, browserify won't transform dependencies. Good to hear we're planning on moving to Babili!

@henri-hulski
Copy link

I have a similar issue but not with a dependency but with node itself.
When running npm run build I get the error:

static/js/main.b4f163eb.js from UglifyJs
Unexpected character '`' [/usr/lib/nodejs/events.js:260,0]

The line 260 in nodejs/events.js contains a template string:

                           `${existing.length} ${type} listeners added. ` +

For more information see cerebral/cerebral#389.

@godmar
Copy link

godmar commented Nov 8, 2016

We are getting the same error (unexpected character backquote in /usr/lib/nodejs/events.js) using react-scripts 0.7.0.

How do we determine why webpack includes /usr/lib/nodejs/events.js in the build? We're using mainly react, redux, and a few bootstrap-related packages. Nothing fancy.

Is there a "debugging mode" in webpack that would tell us why it's trying to including /usr/lib/nodejs/events.js in the build?

@godmar
Copy link

godmar commented Nov 8, 2016

ps: we tracked it down to [email protected] The error disappears if we omit the import for this package.

In general, how do we handle this? Is including certain packages incompatible with create-react-app?

@godmar
Copy link

godmar commented Nov 8, 2016

I figured it out. react-bootstrap-tables uses a package called 'Filter', which imports node.js's EventEmitter, which comes from events.js... I can reproduce the failure by simply adding:

import EventEmitter from 'events';

to CRA's src/index.js.

Isn't this a big problem? Presumably, there are numerous packages/npm that include node.js code that they expect to run in the browser... and with create-react-app, I can't use any of them?

@henri-hulski
Copy link

Here the problem is also EventEmitter.

@gaearon
Copy link
Contributor

gaearon commented Nov 8, 2016

Please file a new issue about EventEmitter and provide code reproducing it.
Thanks!

@godmar
Copy link

godmar commented Nov 9, 2016

Done - see #1023

@mpyw
Copy link

mpyw commented Sep 4, 2017

@gaearon I've got stuck in the related problem, please help me

pre compiled version : a more ES5 compliant build · Issue #4 · moducks/moducks

Why UglifyJSPlugin ignores { resolve: { mainFields: ["main"] } } ?

@mpyw
Copy link

mpyw commented Sep 4, 2017

I also think that create-react-app should require main prior to module by default if it never transpiles dependencies.

@gre
Copy link

gre commented Feb 13, 2018

It would be neat if create-react-app would support ES6 deps. there are a lot of dependencies I'm relying on (especially around cryptocurrencies/ethereum libs, that are primarly written for Node, and Node does support ES6 now!) that currently publish ES6 code (basically just code with let and arrow functions, nothing fancy) and i'm stuck by not being able to build my project.

Only way I have right now is to eject 😭everything was working smoothly except this. context: I don't really want to eject because I'm actually writting a starter kit for writing ethereum apps (create-dapp).

@cory-baker
Copy link

is there no solution to this??????

@jedwards1211
Copy link

@gaearon I agree with @mpyw's suggestion and made the above PR to fix this issue for packages that publish both ES5 and ES6.

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants