UglifyJS throws unexpected token: keyword (const) with node_modules
Asked Answered
D

8

95

A small project I started make use a node module (installed via npm) that declares const variables. Running and testing this project is well, but browserify fails when UglifyJS is executed.

Unexpected token: keyword (const)

Here is a generic Gulp file that I have successfully been using for a few other past projects without this issue (i.e. without that particular node module).

gulpfile.js

'use strict';

const browserify = require('browserify');
const gulp = require('gulp');
const source = require('vinyl-source-stream');
const derequire = require('gulp-derequire');
const buffer = require('vinyl-buffer');
const uglify = require('gulp-uglify');
const sourcemaps = require('gulp-sourcemaps');
const gutil = require('gulp-util');
const path = require('path');
const pkg = require('./package');
const upperCamelCase = require('uppercamelcase');

const SRC_PATH = path.dirname(pkg.main);
const DIST_PATH = path.dirname(pkg.browser);

const INPUT_FILE = path.basename(pkg.main);
const OUTPUT_FILE = path.basename(pkg.browser);

const MODULE_NAME = upperCamelCase(pkg.name);


gulp.task('default', () => {
  // set up the browserify instance on a task basis
  var b = browserify({
    entries: INPUT_FILE,
    basedir: SRC_PATH,
    transform: ['babelify'],
    standalone: MODULE_NAME,
    debug: true
  });

  return b.bundle()
    .pipe(source(OUTPUT_FILE))
    .pipe(buffer())
    .pipe(derequire())
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(uglify())
    .on('error', gutil.log)
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(DIST_PATH))
  ;
});

I have tried fixing this by replace all const to var in that npm-installed module, and everything is fine. So I do not understand the failure.

What's wrong with const? Unless someone uses IE10, all major browsers support this syntax.

Is there a way to fix this without requiring a change to that node module?

Update

I have temporarily (or permanently) replaced UglifyJS with Butternut and seem to work.

Decimate answered 22/11, 2017 at 16:1 Comment(8)
Isn't it a problem with the node version? Don't you need like node 8+ to have const available? (not sure when it was actually introduced)Bradstreet
I have been using const since v4. And I am currently using 8.9.1 LTS.Decimate
Ok that's strange then. What's the error message you are seeing?Bradstreet
@this.lau_ the same error message as in the title, but I added it in the question as well for clarity.Decimate
It doesn't necessarily have to be about the 'const'. It could be one of the modules that you're requiring.Sarto
@Adrián I thought that as well, but I manually edited the file under node_modules and replaced all const to var and UglifyJS didn't complain whatsoever. Then I added a const T = 'foo'; and UglifyJS complained again.Decimate
Maybe you need some other helper module, I remember this same thing happened to me on a new project. I'm not sure how I fixed it but I have this modules as well: --------------------------------------------- "grunt-contrib-concat": "1.0.1", "grunt-contrib-copy": "^1.0.0", "grunt-contrib-jshint": "1.1.0", "grunt-contrib-less": "1.4.0", "grunt-contrib-uglify": "2.0.0", "grunt-contrib-watch": "1.0.0",Sarto
What I mean is you could try to uninstall that UglifyJS version and install the one I installed (if not the same). It should tell you about dependencies if any required.Sarto
I
97

As ChrisR mentionned, UglifyJS does not support ES6 at all.

You need to use terser-webpack-plugin for ES6 (webpack@5 will use this plugin for uglification)

npm install terser-webpack-plugin --save-dev

Then define in your plugins array

const TerserPlugin = require('terser-webpack-plugin')

  new TerserPlugin({
    parallel: true,
    terserOptions: {
      ecma: 6,
    },
  }),

Source

Itagaki answered 16/11, 2018 at 10:32 Comment(6)
Maybe you should suggest npm install --save-dev terser-webpack-plugin.Coagulant
I really appreciate this answer because it reminded me about the terser lib that terser-webpack-plugin uses underneath. Note for others: terser can be used standalone as cli just as uglify-js was (i.e. webpack is not a requirement) which was exactly what I needed.Maupassant
but we need to use webpack to use this solution?Kynan
@enrique depends what you want to do, to build an website that matches real business needs you should definitely give a try to webpack. We had that issue on the webpack community so my answer is well rated but technically you don't need webpack to build ES6 codeItagaki
Terser was cherry-picked to webpack@4 in github.com/webpack/webpack/pull/8392Willettewilley
uglifyjs.terser Ubuntu package worked for me.Johnajohnath
M
42

UglifyJS does not support es6. const is an es6 declaration, so it throws an error.

What is weird is that the package you use does not transpile its files to es5 to be used anywhere.

If you want to still use UglifyJS (to re-use the configuration for example) use the ES6+ compatible version, uglify-es. (Warning: uglify-es is now abandoned.)

And as Ser mentionned, you should now use terser-webpack-plugin.

Mchenry answered 22/11, 2017 at 17:5 Comment(3)
You can also replace gulp-uglifyby gulp-uglify-es: npmjs.com/package/gulp-uglify-esMchenry
UglifyJS does not support es6 . Thank you! I could not find that piece of info anywhere.Blakey
use gulp-terser if migrating to webpack is out of your budget.Swedish
R
13

I had the same issue and the gulp plugin gulp-uglify-es resolved the problem.

I think it's the simpliest decision.

Just install:

npm i gulp-uglify-es --save-dev

after that in your code change only this line

const uglify = require('gulp-uglify');

to this:

const uglify = require('gulp-uglify-es').default;

N.B. property .default is crucial otherwise you'll have an error that uglify is not a function.

As mentioned above and as being part of ES6 const operator can only be processed by more modern es6 gulp plugin "gulp-uglify-es"

The rest of your code no need to be changed.

Best regards!

Roselleroselyn answered 24/3, 2020 at 12:27 Comment(1)
tested and working with "node : v12.14", "gulp cli v2.2.1", "gulp local v4.0.2".Hordein
U
2

I just had this issue with a Gulp project I refactored and for some reason I was having trouble with the official Terser Gulp plugin. This one (gulp-terser) worked with no issues.

Ustulation answered 9/4, 2019 at 2:14 Comment(0)
N
1

Solved an upgrade problem that was returning the same by uninstalling and reinstalling UglifyJS, and ensuring node_modules were aligned.

rm -rf node_modules
npm install uglifyjs-webpack-plugin --save-dev
npm i

(Updated for suggested improvements)

Nieves answered 4/5, 2023 at 10:28 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Arbiter
Hi, I'm confused about using npm install uglifyjs-webpack-plugin --save-dev before removing the node_modules directory. Why don't you start with rm -rf node_modules, then npm install uglifyjs-webpack-plugin --save-dev and finally npm i?Tort
M
0

I have replaced UglifyJS with YUI Compressor JS inside the GUI of PHPStorm.. It works now.

Mclain answered 13/3, 2019 at 10:9 Comment(0)
P
0

I don't really think that this approach is good, but in my case I needed to do this once and forget about that, so I just went to babel's website , transpile es6 to es5 online and replaced the output!

Phosphorescent answered 18/9, 2019 at 17:25 Comment(0)
P
-1

Use uglify-es-webpack-plugin is better

    const UglifyEsPlugin = require('uglify-es-webpack-plugin')



    module.exports = {
    plugins: [
            new UglifyEsPlugin({
                compress:{
                    drop_console: true
                }
            }),
    ] 
    }
Persimmon answered 14/1, 2019 at 17:37 Comment(1)
This is an opinion, please elaborate on why it is better.Mchenry

© 2022 - 2024 — McMap. All rights reserved.