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

Error from UglifyJs - Name Expected #5682

Closed
Imundy opened this issue Sep 15, 2017 · 7 comments
Closed

Error from UglifyJs - Name Expected #5682

Imundy opened this issue Sep 15, 2017 · 7 comments

Comments

@Imundy
Copy link

Imundy commented Sep 15, 2017

Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

A build that succeeds in dev fails when run as a production build.
The specific output I'm getting is:

ERROR in index.js from UglifyJs
Name expected [./request.js:1,31][index.js:84,24]

If the current behavior is a bug, please provide the steps to reproduce.

I've created a gist here.

What is the expected behavior?

The build works in both dev and production

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

This build has failed on both windows and OS X.

@Imundy
Copy link
Author

Imundy commented Sep 15, 2017

Ah after looking at this some more, I've figured something out. It looks like this only happens when the imported file is in a sibling to the root folder. So the following folder structure:

+-src
|--index.js
|--package.json
|--webpack.config.js
|--.babelrc
+-util
|--request.js

When this happens, for some reason the javascript in request.js does not get transpiled by babel which causes Uglify to fail. I'm not sure if this is an issue with webpack, babel-loader, or my configuration.

@Imundy
Copy link
Author

Imundy commented Sep 15, 2017

Ah I have since learned that this is because there is no .babelrc in the sibling directory (util in my example). Seems like this is an issue I am having with babel and not webpack I'm going to close it.

And FWIW, my solution is to pull my webpack config up into the root directory.

@Imundy Imundy closed this as completed Sep 15, 2017
@jakeleboeuf
Copy link

jakeleboeuf commented Dec 15, 2017

I'm running into this error now- not clear to me what you all are suggesting you did to resolve the issue?

image

@OscarBarrett
Copy link

OscarBarrett commented Dec 19, 2017

I ran into this due to a 3rd party module that was providing es6 code (similar to @jakeleboeuf it was related to date-holidays-parser).

The specific error (Name expected) is when UglifyJs tries to parse a const declaration.

A quick fix is to make sure that babel transpile the module(s) in question.
Ideally, modules that are used in the browser should transpile to es5 themselves.

For date-holidays-parser I had to install babel-preset-es2015 (required by date-chinese) and update the rule:

module: {
  rules: [
      test:     /\.jsx?$/,
      include:  [srcPath, /node_modules/],
      exclude:  /node_modules\/(?!date-holidays-parser|date-chinese)/,
      use:  [
        {
          loader: 'babel-loader',
          ...

Edit: For date-holidays-parser there is a transpiled version in the package but by default webpack will check the module field in package.json which in this case points to the source, whereas the main field points to lib. So you can update your webpack resolve.mainFields config to be:

resolve: {
  mainFields: ["browser", "main", "module"],
  ...
}

And then there is no need to transpile the module yourself.

@jgentes
Copy link

jgentes commented Jan 10, 2018

I found the solution to be this:

Don't use the built-in version of uglifyjs-webpack-plugin and install the latest version using npm install -D uglifyjs-webpack-plugin. Add it to your plugins property in your configuration:

const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  plugins: [
    new UglifyJSPlugin()
  ]
}

Taken from https://stackoverflow.com/questions/45870467/error-in-bundle-js-from-uglifyjs-name-expected

@mmrko
Copy link

mmrko commented Nov 28, 2018

Ran into this recently after having moved the config files around. Make sure whatever paths you reference in your Webpack config point to the correct directories/files.

@Volmarg
Copy link

Volmarg commented Jul 15, 2019

I will recommend something else. I had this issue as well. Took me few days and bunch of research to make some progress. Well only thing that helped in my case was opening main index.js file, and comment imports one after another just to find out that only one library breaks compilation.

I don't use that library so i can just remove it - I can't say what's the issue but just a tip from me.

If anyone will be interested for me this were 2 imports:

../../../node/modules/popper.js/dist/popper.js
../../../node/modules/popper.js/dist/popper-utils.min.js

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

6 participants