babel-loader: unexpected token (rest spread operator)
Webpack Config
module.exports = {
entry: './src/public/client.jsx',
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|.cache)/,
loader: 'babel',
query: {
presets: ['react', 'es2015'],
plugins: ['syntax-decorators']
}
}
]
}
}
es2015 includes object rest support, then why am I facing this issue?
UPDATE:
It starts working when i add transform-object-rest-spread
to plugins.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 68
- Comments: 55 (11 by maintainers)
Commits related to this issue
- deps(): add babel plugin to support rest spread operator as per https://github.com/babel/babel-loader/issues/170#issuecomment-257197385 — committed to wmfs/tymly-cardscript-sdk by reecebrend 5 years ago
Here is a .babelrc that works for doing things like
.babelrc
You can install the two plugins with:
npm install --save-dev babel-plugin-transform-es2015-destructuring
&
npm install --save-dev babel-plugin-transform-object-rest-spread
As @janzenz mentioned for me adding
stage-0
worked I did not needtransform-object-rest-spread
andtransform-es2015-destructuring
{ "presets": ["react", "es2015", "stage-0"] }
In my case I just installed
stage-0
and added it on my.babelrc
file, that solved for me.You didn’t need them because you are already using the es2015 preset.
I’d recommend staying away from stage-0; use stage-3, which also includes this feature, but includes less features that have a high risk of changing dramatically.
EDIT: better / more up-to-date advice: don’t use any preset besides
env
,react
orflow
(should you use either of their features); avoid yearly or stage presets. If you need to use an experimental feature that’s not supported by those 3 presets, use its plugin directly.@iDVB
{...obj}
is object rest/spread and it’s not in es2015. It’s an experimental feature. I mentioned above you needtransform-object-rest-spread plugin or stage-2 preset
. It’s not included in es2015 because it’s not in the spec yet and not part of react.transform-es2015-destructuring involves
a(...a), [...d]
- over lists/arrays....
with an object is differentWhat you’re trying to do is a stage2 feature. You can either add the plugin as you have done or add the entire stage2 preset. For more information, see https://github.com/sebmarkbage/ecmascript-rest-spread.
@Kovensky Thank you!
If you’re using
@babel/preset-env
you’ll neednpm i -D @babel/plugin-transform-destructuring @babel/plugin-proposal-object-rest-spread
so what is the minimal config I need for this?
I tried this:
but still get errors
After many hours searching for info and lots of trial and error, I have tried every combination of plugins and presets possible, yet none of them have worked. Can someone please confirm a set up that works? I am running Node V6.2.2
But its not documented anywhere, that this is how I should be using it.
tried the stage-3 preset, but it still does not work for me.
transform-object-rest-spread
plugin or stage-2 preset. you can try it in the repl tooFor future queriers:
If you’re going that route, the package is now called
babel-plugin-transform-object-rest-spread
. Also worked for me.Things that are stage-4 are no longer proposals – they are part of the language.
It not being in
babel-preset-env
is because the preset hasn’t been updated yet. The babel 7 version (@babel/preset-env
) supports ashippedProposals
option that will enable it; the missing update is to have it enabled by default instead of enabled byshippedProposals
.To clarify,
object-rest-spread
is still a proposal, and right now is a stage 4 proposal, so that’s why it isn’t inbabel-preset-env
and we need the plugin?I thought that all the spread operator usages were in the same stage (or same proposal). It’s weird that we can use spread/rest in arrays and fns but in objects not… I thought that the spread operator worked on any iterable (that cover objects)…
My conscience got the better of me, sorry for being snippy. You’re right, I know better than to ask those kinds of questions here. Thanks again, though for your explanation, it really did help out a lot!
As I stated above “I realize this is not stack overflow”. If answering my question is a waste of your time, then don’t answer it. My original purpose was to report a bug. It turns out I made a mistake, as we all, I’m sure you included, do. Thank you for the info you’ve given. It was helpful.
Agreed with @Kovensky, I started a project about 2 months ago installing
stage-0
. I changed laptops recently and afternpm install
thestage-0
gave me massive issues withspread
operator.For me @kareniel answer fixed the issue.
I had a similar issue. I am using package.json to define my plugins and presets.
"babel": { "presets": [ "env"], "plugins": [] }
I found that the .babelrc was overwriting everything I entered in package.json. Once I deleted the .babelrc file, the “env” preset alone fixed my issue of using spread syntax.Added stage-0 by
yarn add babel-preset-stage-0
and changed the.babelrc
toworked for me. Thanks @StevenIseki 😃
@QuantumInformation Please drop by one of our support channels: https://github.com/babel/babel#looking-for-support That config should be fine, so it’s likely an issue elsewhere.
This might help anyone coming around this issue, using babel 7: https://babeljs.io/docs/en/babel-preset-stage-0 https://babeljs.io/blog/2018/07/27/removing-babels-stage-presets
As mentioned before, I believe that
@babel/plugin-proposal-object-rest-spread
is the solution 👍Facing the same problem, tried all the above suggestions. Nothing worked. Is there any perfect solution to this problem?
Tried every solution mentioned above and none of them works for me:
37:21 is on
{
atreturn [{...array[i]}]
.babelrc:
Something else I should do or can provide?
Good Job!!!
Yeah, realized that. Got used to Typescript where everything works. I’m using stage-0 now
env and es2017 do not contain object rest spread. Object-rest-spread is still an experimental feature which not yet finished the specification process. If you want to use it install babel-preset-stage-3 or the plugin itself. see @kareniel s comment above