{"id":580,"hash":"c89bd1f99808923527df398640fb4404eb89945097afde3b0f25dd7f2e674d13","pattern":"UglifyJS throws unexpected token: keyword (const) with node_modules","full_message":"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.\n\n  Unexpected token: keyword (const)\n\nHere 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).\n\ngulpfile.js\n\n'use strict';\n\nconst browserify = require('browserify');\nconst gulp = require('gulp');\nconst source = require('vinyl-source-stream');\nconst derequire = require('gulp-derequire');\nconst buffer = require('vinyl-buffer');\nconst uglify = require('gulp-uglify');\nconst sourcemaps = require('gulp-sourcemaps');\nconst gutil = require('gulp-util');\nconst path = require('path');\nconst pkg = require('./package');\nconst upperCamelCase = require('uppercamelcase');\n\nconst SRC_PATH = path.dirname(pkg.main);\nconst DIST_PATH = path.dirname(pkg.browser);\n\nconst INPUT_FILE = path.basename(pkg.main);\nconst OUTPUT_FILE = path.basename(pkg.browser);\n\nconst MODULE_NAME = upperCamelCase(pkg.name);\n\ngulp.task('default', () => {\n  // set up the browserify instance on a task basis\n  var b = browserify({\n    entries: INPUT_FILE,\n    basedir: SRC_PATH,\n    transform: ['babelify'],\n    standalone: MODULE_NAME,\n    debug: true\n  });\n\n  return b.bundle()\n    .pipe(source(OUTPUT_FILE))\n    .pipe(buffer())\n    .pipe(derequire())\n    .pipe(sourcemaps.init({loadMaps: true}))\n    .pipe(uglify())\n    .on('error', gutil.log)\n    .pipe(sourcemaps.write('.'))\n    .pipe(gulp.dest(DIST_PATH))\n  ;\n});\n\nI 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.\n\nWhat's wrong with const? Unless someone uses IE10, all major browsers support this syntax.\n\nIs there a way to fix this without requiring a change to that node module?\n\nUpdate\n\nI have temporarily (or permanently) replaced UglifyJS with Butternut and seem to work.","ecosystem":"npm","package_name":"npm","package_version":null,"solution":"As ChrisR mentionned, UglifyJS does not support ES6 at all.\n\nYou need to use terser-webpack-plugin for ES6 (webpack@5 will use this plugin for uglification)\n\nnpm install terser-webpack-plugin --save-dev\n\nThen define in your plugins array\n\nconst TerserPlugin = require('terser-webpack-plugin')\n\n  new TerserPlugin({\n    parallel: true,\n    terserOptions: {\n      ecma: 6,\n    },\n  }),\n\nSource","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/47439067/uglifyjs-throws-unexpected-token-keyword-const-with-node-modules","votes":94,"created_at":"2026-04-19T04:51:22.579102+00:00","updated_at":"2026-04-19T04:51:22.579102+00:00"}