0

I am following a react tutorial by Brian holt and i need to import .json file inside my react component like this : code

When I try to build my project i was getting

ERROR in ./data.json
Module build failed: SyntaxError: Unexpected token, expected

like this: Error caption in terminal

at first i thought that it was an eslint issue but it seems that it happens on the build step, i tried adding a json-loader to webpack but without any success.

Webpack Version: 2.6.1

Babel Core Version: 6.24.1

Babel Loader Version: 7.0.0, this is my webpack config file :

const path = require('path')
module.exports = {
   context: __dirname,
   entry: './js/clientApp',
   devtool: 'cheap-eval-source-map',
   output: {
      path: path.join(__dirname, 'public'),
      filename: 'bundle.js',
      publicPath: '/assets/'
   },
   resolve: {
      extensions: ['.js', '.jsx', ',json']
   },
   devServer: {
      publicPath: '/public/',
      port: 2110,
      open: true,
      historyApiFallback: true
   },
   stats: {
      colors: true,
      reasons: true,
      chunks: true
   },
   module: {
      rules: [
         {
            enforce: 'pre',
            test: /\.jsx?/,
            loader: 'eslint-loader',
            exclude: /node_modules/
         },

         {
            test: /\.jsx?/,
            loader: 'babel-loader'
         }
      ]
   }
}

and this is my .babelrc file :

{
    "presets": [
        "react", ["env", {
            "targets": {
                "browsers": "last 2 versions"
            },
            "loose": true,
            "modules": false
        }]
    ]
}

Any suggestions please ?

16
  • 1
    Can you share your data.json Commented Mar 18, 2018 at 20:59
  • Possible duplicate of es6 modules implementation, how to load a json file Commented Mar 18, 2018 at 23:04
  • you need to use json-loader, check this answer: stackoverflow.com/a/33650470/6836839 Commented Mar 18, 2018 at 23:06
  • 1
    @btzr i tried installing the json-loader and the error persists !! Commented Mar 18, 2018 at 23:14
  • 1
    i ve already posted a caption of the error and this is how it is ERROR in ./data.json Module build failed: SyntaxError: Unexpected token, expected ; (2:11) 1 | { > 2 | "shows": [ | ^ 3 | 4 | { 5 | "title": "Atlanta", Commented Mar 18, 2018 at 23:31

1 Answer 1

2

Regular Expression

The test property identifies which file or files should be transformed.

The problem is the regular expression on your rules:

{ test: /\.jsx?/ }

Your telling webpack to use babel-loader with any file extension starting with .js or .jsx

So as you can see .json match the test.

Solution

$ - matches anything until the end of the string

To fix this just replace ? with $, see the example below:

 module: {
      rules: [
         {
            test: /\.jsx$/,
            loader: 'babel-loader'
         }
      ]
   }
Sign up to request clarification or add additional context in comments.

1 Comment

Thankkkkkk you so much @btzr for your help, that was really a nightmare for me xdxd.

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.