I'm using function form of "use strict" and don't want global form which Babel adds after transpilation. The problem is I'm using some libraries that aren't using "use strict" mode and it might throw error after scripts are concatenated
-
What version of Babel are you using? And how are you using Babel? – T.J. Crowder Nov 20 '15 at 07:48
-
1Babel 6. They have removed blacklist option in Babel 6. – ani h Nov 20 '15 at 08:00
-
1Babel 7 https://stackoverflow.com/questions/52827968/babel-7-how-to-prevent-adding-of-strict-mode – Félix Paradis Jul 31 '19 at 18:34
16 Answers
As it has already been mentioned for Babel 6, it's the transform-es2015-modules-commonjs
preset which adds strict mode.
In case you want to use the whole es2015
preset without module transformations, put this in your .babelrc
file:
{
"presets": [
["es2015", { "modules": false }]
]
}
This will disable modules and strict mode, while keeping all other es2015 transformations enabled.
-
Works great with webpack 2 which wants this babel config anyways to leverage tree shaking. Reference: https://medium.freecodecamp.com/tree-shaking-es6-modules-in-webpack-2-1add6672f31b – Jason Kuhrt Mar 31 '17 at 02:42
-
3
-
I'm using `gulp-babel` version `^6.1.3` and this worked for me with a slight variation, hope this can help others in my situation: `{ "presets": [ ["babel-preset-env", { "modules": false }] ] }` – vcoppolecchia Sep 18 '19 at 12:43
Babel 5
You'd blacklist "useStrict"
. For instance here's an example in a Gruntfile:
babel: {
options: {
blacklist: ["useStrict"],
// ...
},
// ...
}
Babel 6
Since Babel 6 is fully opt-in for plugins now, instead of blacklisting useStrict
, you just don't include the strict-mode
plugin. If you're using a preset that includes it, I think you'll have to create your own that includes all the others, but not that one.

- 29,846
- 15
- 139
- 192

- 1,031,962
- 187
- 1,923
- 1,875
-
2Thanks! Your answer clarified my doubt but I'm still facing the problem. So here are few more details: I'm using **gulp-babel** and have not used **strict-mode** plugin neither using any preset which internally uses **strict-mode** plugin. – ani h Nov 20 '15 at 10:06
-
@anih: Apologies, I haven't used Babel with Gulp and haevn't (yet) used Babel 6, either. – T.J. Crowder Nov 20 '15 at 12:30
-
1I'm using Babel6 and [preset-es2015](https://babeljs.io/docs/plugins/preset-es2015/). I do not see [strict-mode](http://babeljs.io/docs/plugins/transform-strict-mode/)-plugin in that preset. Any idea? – artin Dec 04 '15 at 15:22
-
16It's the ["transform-es2015-modules-commonjs" plugin](http://babeljs.io/docs/plugins/transform-es2015-modules-commonjs/) (a plugin in "es2015" preset) which is what's adding the "use strict". – laggingreflex Dec 19 '15 at 03:00
-
8I have just published `babel-preset-es2015-nostrict` package. It basically has `transform-es2015-modules-commonjs` plugin commented out, everything else just as in the normal `babel-preset-es2015@6.6.0` which I have forked. – alexykot May 10 '16 at 17:30
-
-
I don't use gulp, I use webpack. Also actually commenting out didn't work that well, as that module is doing more than just adding strict statement. Though I've managed to turn it off through options, so preset `babel-preset-es2015-nostrict` now works just fine. – alexykot May 11 '16 at 05:57
-
@T.J.Crowder I'm running into an issue with Angular and gulp-babel - http://stackoverflow.com/questions/40772301/gulp-babel-with-angular-error-module-app-is-not-available - produces module not available error - I've tried these things without luck – user3871 Nov 26 '16 at 15:26
-
@alexykot how would you reference that in `gulp`? ` `.pipe(babel({ presets: ['es2015-no-strict'] }))` ? – user3871 Nov 26 '16 at 15:46
-
For ES6, Making your own preset is extra work. Look at the plugin Alan Pierce suggests in his answer for something easier. Mergin his and this answer would be appropriate i think. :) – Vargr Apr 11 '19 at 11:42
There's now a babel plugin that you can add to your config that will remove strict mode: babel-plugin-transform-remove-strict-mode
. It's a little ugly in that the "use strict"
gets added and then removed, but it makes the config much nicer.
Docs are in the GitHub repo: https://github.com/genify/babel-plugin-transform-remove-strict-mode
Your .babelrc ends up looking like this:
{
"presets": ["env"],
"plugins": ["transform-remove-strict-mode"]
}

- 4,083
- 2
- 22
- 21
I also came accross this rather ridiculous limitation that you cannot disable or overwrite settings from an existing preset, and have resorted to using this preset instead: https://www.npmjs.com/package/babel-preset-es2015-without-strict

- 4,165
- 1
- 44
- 35
-
Note that this answer is now outdated, and we've since switched to using `@babel/preset-env` – Adam Reis Mar 12 '19 at 20:47
plugins: [
[
require("@babel/plugin-transform-modules-commonjs"),
{
strictMode: false
}
],
]

- 3,426
- 2
- 17
- 36

- 113
- 1
- 6
-
This worked for me! I use gulp-babel. But, how to find this exact way? where is it described? – Vsevolod Azovsky Jun 14 '19 at 06:47
-
-
1it is described here https://babeljs.io/docs/en/babel-plugin-transform-strict-mode but still, not 100% understandable. Open issue for documentation: https://github.com/babel/babel/issues/7910 – Yair Kukielka Jul 17 '20 at 12:45
You can tell babel that your code is a script with:
sourceType: "script"
in your babel config file. This will not add use strict
. See sourceType option docs
Source: https://github.com/babel/babel/issues/7910#issuecomment-388517631

- 10,686
- 1
- 38
- 46
Babel 6 + es2015
We can disabled babel-plugin-transform-es2015-modules-commonjs
to require babel-plugin-transform-strict-mode
.
So comment the following code in node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js
at 151 line
//inherits: require("babel-plugin-transform-strict-mode"),

- 14,387
- 17
- 74
- 104

- 1,921
- 16
- 17
-
This works for me, is it possible to do the same thing with an option ? – Kévin Berthommier May 31 '16 at 13:12
-
-
2IMO this is the better answer. It means putting a (godawful) hack in a build script, but that way I don't have to rely on a forked preset (that is already out of date) – AnilRedshift Jun 14 '16 at 16:16
-
8Wait what? So, this proposed solution is to edit files that will be overwritten next time you npm install? How about when you deploy? – Félix Adriyel Gagnon-Grenier Mar 02 '18 at 15:07
-
You should not be editing any files within node_modules since these will be overwritten. – Michael Sep 19 '19 at 15:23
-
for the babel 7 plugin there is an option, see link https://babeljs.io/docs/en/babel-plugin-transform-strict-mode – Yair Kukielka Jul 13 '20 at 13:03
just change .babelrc
solution
if you don't want to change any npm modules, you can use .babelrc
ignore like this
{
"presets": ["es2015"],
"ignore": [
"./src/js/directive/datePicker.js"
]
}
ignore that file, it works for me!
the ignored file that can't use 'use strict'
is old code, and do not need to use babel to transform it!

- 669
- 7
- 8
Personally, I use the gulp-iife plugin and I wrap IIFEs around all my files. I noticed that the babel plugin (using preset es2015) adds a global "use strict" as well. I run my post babel code through the iife stream plugin again so it nullifies what babel did.
gulp.task("build-js-source-dev", function () {
return gulp.src(jsSourceGlob)
.pipe(iife())
.pipe(plumber())
.pipe(babel({ presets: ["es2015"] }))// compile ES6 to ES5
.pipe(plumber.stop())
.pipe(iife()) // because babel preset "es2015" adds a global "use strict"; which we dont want
.pipe(concat(jsDistFile)) // concat to single file
.pipe(gulp.dest("public_dist"))
});

- 1,361
- 1
- 15
- 22
-
2This should be the solution for people running into this issue with Gulp. Nice suggestion. – May 07 '16 at 00:00
-
Anyone know how to integrate iife with a browserify/babelify setup? – Prasad Silva May 26 '16 at 19:05
-
1Way too many of these automation tools to keep track of for me =) – Brian Schermerhorn May 26 '16 at 19:21
-
I used the shihongzhi solution and it worked. But i'm using Gulp also, so is this better? Is there any configuration I need to set with iife or just install it? – sandrina-p Aug 23 '16 at 08:53
-
This is not grammatically correct, but will basically work for both Babel 5 and 6 without having to install a module that removes another module.
code.replace(/^"use strict";$/, '')

- 14,688
- 7
- 55
- 109
Since babel 6 you can install firstly babel-cli (if you want to use Babel from the CLI ) or babel-core (to use the Node API). This package does not include modules.
npm install --global babel-cli
# or
npm install --save-dev babel-core
Then install modules that you need. So do not install module for 'strict mode' in your case.
npm install --save-dev babel-plugin-transform-es2015-arrow-functions
And add installed modules in .babelrc file like this:
{
"plugins": ["transform-es2015-arrow-functions"]
}
See details here: https://babeljs.io/blog/2015/10/31/setting-up-babel-6

- 145
- 1
- 7
For babel 6 instead of monkey patching the preset and/or forking it and publishing it, you can also just wrap the original plugin and set the strict
option to false
.
Something along those lines should do the trick:
const es2015preset = require('babel-preset-es2015');
const commonjsPlugin = require('babel-plugin-transform-es2015-modules-commonjs');
es2015preset.plugins.forEach(function(plugin) {
if (plugin.length && plugin[0] === commonjsPlugin) {
plugin[1].strict = false;
}
});
module.exports = es2015preset;

- 4,643
- 1
- 27
- 34
Please use "es2015-without-strict" instead of "es2015". Don't forget you need to have package "babel-preset-es2015-without-strict" installed. I know it's not expected default behavior of Babel, please take into account the project is not mature yet.

- 1,547
- 14
- 23
I just made a script that runs in the Node and removes "use strict"; in the selected file.
file: script.js:
let fs = require('fs');
let file = 'custom/path/index.js';
let data = fs.readFileSync(file, 'utf8');
let regex = new RegExp('"use\\s+strict";');
if (data.match(regex)){
let data2 = data.replace(regex, '');
fs.writeFileSync(file, data2);
console.log('use strict mode removed ...');
}
else {
console.log('use strict mode is missing .');
}
node ./script.js

- 260
- 4
- 14
if you are using https://babeljs.io/repl (v7.8.6
as of this writing), you can remove "use strict";
by selecting Source Type -> Module.

- 26,371
- 26
- 130
- 172
Using plugins or disabling modules and strict mode as suggested in the @rcode's answer didn't work for me.
But, changing the target from es2015
|es6
to es5
in tsconfig.json
file as suggested by @andrefarzart in this GitHub answer fixed the issue.
// tsconfig.json file
{
// ...
"compilerOptions": {
// ...
"target": "es5", // instead of "es2015"
}

- 1,985
- 2
- 19
- 22