Search code examples
javascriptreactjswebpackuglifyjscreate-react-app

npm run build uglifyjs: SyntaxError: Unexpected token: operator (>)


I am trying to optimize Create-react-app for production using following command:

npm run build

But it is giving me following error:

static/js/main.9c091e05.js from UglifyJs
SyntaxError: Unexpected token: operator (>) [./~/http-build-query/index.js:16,0]

That might be because of module http-build-query but that module is too necessary for my app.

I have also tried yarn build and that is also giving me same error.

EDIT

I just knew by googling that ES6 syntax is not supported by npm run build. So the question is how to build ES6 syntax with npm run build ?


Solution

  • The reason this is happening is because the ES6 code in your node_modules folder is not handled by create-react-app - it assumes they have done this themselves (which they should).

    Option 1

    Stop using that package, and find a better one.

    Option 2

    Copy the code from the package to your own source files so babel has access to it

    Option 3

    Eject create-react-app and customize the webpack config yourself, including a test for the node_modules folder (can really hinder your build time)

    Option 4

    Overriding the webpack config from create-react-app to accomplish Option 3 without ejecting. Note this is complicated and Option 1 or Option 2 is what you should go for

    Example of overriding can be found here: https://daveceddia.com/customize-create-react-app-webpack-without-ejecting/