UglifyJS throws unexpected token: keyword (const) with node_modules
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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.
javascript npm gulp babeljs uglifyjs2
|
show 3 more comments
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.
javascript npm gulp babeljs uglifyjs2
Isn't it a problem with the node version? Don't you need like node 8+ to haveconst
available? (not sure when it was actually introduced)
– laurent
Nov 22 '17 at 16:03
I have been usingconst
since v4. And I am currently using 8.9.1 LTS.
– Yanick Rochon
Nov 22 '17 at 16:05
Ok that's strange then. What's the error message you are seeing?
– laurent
Nov 22 '17 at 16:06
@this.lau_ the same error message as in the title, but I added it in the question as well for clarity.
– Yanick Rochon
Nov 22 '17 at 16:10
It doesn't necessarily have to be about the 'const'. It could be one of the modules that you're requiring.
– James
Nov 22 '17 at 16:15
|
show 3 more comments
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.
javascript npm gulp babeljs uglifyjs2
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.
javascript npm gulp babeljs uglifyjs2
javascript npm gulp babeljs uglifyjs2
edited Nov 22 '17 at 16:50
Yanick Rochon
asked Nov 22 '17 at 16:01
Yanick RochonYanick Rochon
34.8k2091160
34.8k2091160
Isn't it a problem with the node version? Don't you need like node 8+ to haveconst
available? (not sure when it was actually introduced)
– laurent
Nov 22 '17 at 16:03
I have been usingconst
since v4. And I am currently using 8.9.1 LTS.
– Yanick Rochon
Nov 22 '17 at 16:05
Ok that's strange then. What's the error message you are seeing?
– laurent
Nov 22 '17 at 16:06
@this.lau_ the same error message as in the title, but I added it in the question as well for clarity.
– Yanick Rochon
Nov 22 '17 at 16:10
It doesn't necessarily have to be about the 'const'. It could be one of the modules that you're requiring.
– James
Nov 22 '17 at 16:15
|
show 3 more comments
Isn't it a problem with the node version? Don't you need like node 8+ to haveconst
available? (not sure when it was actually introduced)
– laurent
Nov 22 '17 at 16:03
I have been usingconst
since v4. And I am currently using 8.9.1 LTS.
– Yanick Rochon
Nov 22 '17 at 16:05
Ok that's strange then. What's the error message you are seeing?
– laurent
Nov 22 '17 at 16:06
@this.lau_ the same error message as in the title, but I added it in the question as well for clarity.
– Yanick Rochon
Nov 22 '17 at 16:10
It doesn't necessarily have to be about the 'const'. It could be one of the modules that you're requiring.
– James
Nov 22 '17 at 16:15
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)– laurent
Nov 22 '17 at 16:03
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)– laurent
Nov 22 '17 at 16:03
I have been using
const
since v4. And I am currently using 8.9.1 LTS.– Yanick Rochon
Nov 22 '17 at 16:05
I have been using
const
since v4. And I am currently using 8.9.1 LTS.– Yanick Rochon
Nov 22 '17 at 16:05
Ok that's strange then. What's the error message you are seeing?
– laurent
Nov 22 '17 at 16:06
Ok that's strange then. What's the error message you are seeing?
– laurent
Nov 22 '17 at 16:06
@this.lau_ the same error message as in the title, but I added it in the question as well for clarity.
– Yanick Rochon
Nov 22 '17 at 16:10
@this.lau_ the same error message as in the title, but I added it in the question as well for clarity.
– Yanick Rochon
Nov 22 '17 at 16:10
It doesn't necessarily have to be about the 'const'. It could be one of the modules that you're requiring.
– James
Nov 22 '17 at 16:15
It doesn't necessarily have to be about the 'const'. It could be one of the modules that you're requiring.
– James
Nov 22 '17 at 16:15
|
show 3 more comments
4 Answers
4
active
oldest
votes
November 2018 update :
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
1
Maybe you should suggestnpm install --save-dev terser-webpack-plugin
.
– Rafa
Nov 19 '18 at 16:48
add a comment |
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
.
2
You can also replacegulp-uglify
bygulp-uglify-es
: npmjs.com/package/gulp-uglify-es
– ChrisR
Nov 22 '17 at 17:08
add a comment |
Use uglify-es-webpack-plugin is better
const UglifyEsPlugin = require('uglify-es-webpack-plugin')
module.exports = {
plugins: [
new UglifyEsPlugin({
compress:{
drop_console: true
}
}),
]
}
1
This is an opinion, please elaborate on why it is better.
– ChrisR
Jan 17 at 15:55
add a comment |
I have replaced UglifyJS
with YUI Compressor JS
inside the GUI of PHPStorm.. It works now.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47439067%2fuglifyjs-throws-unexpected-token-keyword-const-with-node-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
November 2018 update :
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
1
Maybe you should suggestnpm install --save-dev terser-webpack-plugin
.
– Rafa
Nov 19 '18 at 16:48
add a comment |
November 2018 update :
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
1
Maybe you should suggestnpm install --save-dev terser-webpack-plugin
.
– Rafa
Nov 19 '18 at 16:48
add a comment |
November 2018 update :
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
November 2018 update :
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
edited Nov 20 '18 at 10:24
answered Nov 16 '18 at 10:32
SerSer
1,128923
1,128923
1
Maybe you should suggestnpm install --save-dev terser-webpack-plugin
.
– Rafa
Nov 19 '18 at 16:48
add a comment |
1
Maybe you should suggestnpm install --save-dev terser-webpack-plugin
.
– Rafa
Nov 19 '18 at 16:48
1
1
Maybe you should suggest
npm install --save-dev terser-webpack-plugin
.– Rafa
Nov 19 '18 at 16:48
Maybe you should suggest
npm install --save-dev terser-webpack-plugin
.– Rafa
Nov 19 '18 at 16:48
add a comment |
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
.
2
You can also replacegulp-uglify
bygulp-uglify-es
: npmjs.com/package/gulp-uglify-es
– ChrisR
Nov 22 '17 at 17:08
add a comment |
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
.
2
You can also replacegulp-uglify
bygulp-uglify-es
: npmjs.com/package/gulp-uglify-es
– ChrisR
Nov 22 '17 at 17:08
add a comment |
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
.
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
.
edited Nov 19 '18 at 10:20
answered Nov 22 '17 at 17:05
ChrisRChrisR
2,2541517
2,2541517
2
You can also replacegulp-uglify
bygulp-uglify-es
: npmjs.com/package/gulp-uglify-es
– ChrisR
Nov 22 '17 at 17:08
add a comment |
2
You can also replacegulp-uglify
bygulp-uglify-es
: npmjs.com/package/gulp-uglify-es
– ChrisR
Nov 22 '17 at 17:08
2
2
You can also replace
gulp-uglify
by gulp-uglify-es
: npmjs.com/package/gulp-uglify-es– ChrisR
Nov 22 '17 at 17:08
You can also replace
gulp-uglify
by gulp-uglify-es
: npmjs.com/package/gulp-uglify-es– ChrisR
Nov 22 '17 at 17:08
add a comment |
Use uglify-es-webpack-plugin is better
const UglifyEsPlugin = require('uglify-es-webpack-plugin')
module.exports = {
plugins: [
new UglifyEsPlugin({
compress:{
drop_console: true
}
}),
]
}
1
This is an opinion, please elaborate on why it is better.
– ChrisR
Jan 17 at 15:55
add a comment |
Use uglify-es-webpack-plugin is better
const UglifyEsPlugin = require('uglify-es-webpack-plugin')
module.exports = {
plugins: [
new UglifyEsPlugin({
compress:{
drop_console: true
}
}),
]
}
1
This is an opinion, please elaborate on why it is better.
– ChrisR
Jan 17 at 15:55
add a comment |
Use uglify-es-webpack-plugin is better
const UglifyEsPlugin = require('uglify-es-webpack-plugin')
module.exports = {
plugins: [
new UglifyEsPlugin({
compress:{
drop_console: true
}
}),
]
}
Use uglify-es-webpack-plugin is better
const UglifyEsPlugin = require('uglify-es-webpack-plugin')
module.exports = {
plugins: [
new UglifyEsPlugin({
compress:{
drop_console: true
}
}),
]
}
answered Jan 14 at 17:37
NEO ViSiONNEO ViSiON
1
1
1
This is an opinion, please elaborate on why it is better.
– ChrisR
Jan 17 at 15:55
add a comment |
1
This is an opinion, please elaborate on why it is better.
– ChrisR
Jan 17 at 15:55
1
1
This is an opinion, please elaborate on why it is better.
– ChrisR
Jan 17 at 15:55
This is an opinion, please elaborate on why it is better.
– ChrisR
Jan 17 at 15:55
add a comment |
I have replaced UglifyJS
with YUI Compressor JS
inside the GUI of PHPStorm.. It works now.
add a comment |
I have replaced UglifyJS
with YUI Compressor JS
inside the GUI of PHPStorm.. It works now.
add a comment |
I have replaced UglifyJS
with YUI Compressor JS
inside the GUI of PHPStorm.. It works now.
I have replaced UglifyJS
with YUI Compressor JS
inside the GUI of PHPStorm.. It works now.
edited Mar 13 at 10:28
Azik Abdullah
6,6651264112
6,6651264112
answered Mar 13 at 10:09
SjoerdSjoerd
419
419
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47439067%2fuglifyjs-throws-unexpected-token-keyword-const-with-node-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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)– laurent
Nov 22 '17 at 16:03
I have been using
const
since v4. And I am currently using 8.9.1 LTS.– Yanick Rochon
Nov 22 '17 at 16:05
Ok that's strange then. What's the error message you are seeing?
– laurent
Nov 22 '17 at 16:06
@this.lau_ the same error message as in the title, but I added it in the question as well for clarity.
– Yanick Rochon
Nov 22 '17 at 16:10
It doesn't necessarily have to be about the 'const'. It could be one of the modules that you're requiring.
– James
Nov 22 '17 at 16:15