Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack: Parse json failed with webpack4 #614

Closed
iqingting opened this issue May 2, 2018 · 4 comments
Closed

Webpack: Parse json failed with webpack4 #614

iqingting opened this issue May 2, 2018 · 4 comments

Comments

@iqingting
Copy link

iqingting commented May 2, 2018

I'm submitting a bug report

Webpack Version:
4.6.0

Babel Core Version:
6.26.3

Babel Loader Version:
7.1.4

Please tell us about your environment:
OSX 10.13.4

Current behavior:

This error occurs after using babel-loader.

// index.js
import json from './index.json'
// index.json
{
  "key": "value"
}
ERROR in ./index.json
Module build failed: SyntaxError: Unexpected token, expected ; (2:7)

  1 | {
> 2 |   "key": "value"
    |        ^
  3 | }
  4 |

Expected/desired behavior:
parse json as expected.

  • What is the motivation / use case for changing the behavior?
git clone git@github.com:rguanghui/webpack-demo.git
npm i
npm run build
@alexzherdev
Copy link

I believe your issue is that the /\.js/ test in webpack config doesn't have a $ at the end, so it also matches .json files.

@iqingting
Copy link
Author

sorry for my careless

@FlaviooLima
Copy link

I know that is probably not related. But if I got here probably someone will too.
But I did get a similar error and @sakulstra solution worked 100% for me:
jestjs/jest#8426 (comment)


just a small addition for people accidentally stepping over this:

if you've been using

transform: {
    ...,
    '*.': 'babel-jest'
}
28.8 was a breaking change as it will break your json imports. You might want to e.g. restrict the transform to only transform js files.

transform: {
   ...,
    '^.+\\.(js)$': 'babel-jest'
}

igunultas referenced this issue in raicem/laravel-mix-react-typescript-extension May 13, 2020
@guanzo
Copy link

guanzo commented May 14, 2020

I believe your issue is that the /.js/ test in webpack config doesn't have a $ at the end, so it also matches .json files.

😭😭😭

Lessons learned:

  1. Always define a condition for a webpack loader. If you don't, the loader will apply to EVERY file... not what you want.
  2. If possible, always use ^ and $ in your regex, to restrict the scope of the selector.

The devil is in the details 😭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants