4

Having a small problem with the webpack. I have the following code which is not mine:

pointer.files = {
    ...pointer.files,
    [file]: 1
};

And I get the following error:

Module build failed: SyntaxError: Unexpected token (84:28)

  82 |                         pointer.files = pointer.files || {};
  83 |                         pointer.files = {
> 84 |                             ...pointer.files,
     |                             ^
  85 |                             [file]: 1
  86 |                         };
  87 |                     });

I usually don't use the ... so I'm not sure what the problem. What would be the best way to replace the need of ... with another syntax approach in order to make it work?

5
  • Another option would be to use Object.assign({[file]: 1}, pointer.files) Commented Aug 13, 2020 at 14:17
  • I think you are missing the property name to which assign ...pointer.files value Commented Aug 13, 2020 at 14:20
  • what is pointer.files when not {}? Commented Aug 13, 2020 at 14:21
  • 3
    @DavideBulbarelli you can spread objects into other objects (as of ES9), so it doesn't need a property name Commented Aug 13, 2020 at 14:22
  • 1
    Sounds like babel is not configured correctly. Commented Aug 13, 2020 at 14:23

1 Answer 1

2

I had a similar problem. I used npm run watch to build my javascript files and received the following error on the command line:

ERROR in ./react/src/common/components/RowPlaceholder.js
Module build failed: SyntaxError: Unexpected token (14:6)

  12 |       position: 'absolute',
  13 |       background: 'white',
> 14 |       ...style
     |       ^
  15 |     }}
  16 |   />
  17 | const buildStencil = stencil =>

 @ ./react/src/common/components/List.js 27:22-49

In the browser, the console showed the following error:

Uncaught Error: Cannot find module "./RowPlaceholder"
    webpackMissingModule List.js:5
    <anonymous> List.js:5
    Webpack 9

I could solve the problem by applying this solution based on other posts on Stackoverflow:

  1. npm install babel-plugin-transform-object-rest-spread

  2. Create a .babelrc in the root directory of the project and add these lines

(strange bug on Stackoverflow right now: when I leave this line of my answer empty the codeblock below is not rendered as code)

{
   "plugins": [
    "babel-plugin-transform-object-rest-spread",
  ],
}

The three dots are known as spread syntax, by the way.

Sign up to request clarification or add additional context in comments.

1 Comment

There are 2 extra commas in the .babelrc which will cause the build to error out. Even with the commas removed, it unfortunately did not fix the error for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.