32

I am working through the Lynda tutorial for React.js Essential training and am running into a problem with chapter 13 - Loading JSON with webpack.

When I try and compile and start the server I am getting the following error:

ERROR in ./src/titles.json
Module parse failed: Unexpected token m in JSON at position 0
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token m in JSON at position 0
    at JSON.parse (<anonymous>)
    at JsonParser.parse (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\lib\JsonParser.js:15:21)
    at doBuild.err (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\lib\NormalModule.js:367:32)
    at runLoaders (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\lib\NormalModule.js:264:12)
    at C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:370:3
    at iterateNormalLoaders (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:211:10)
    at iterateNormalLoaders (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:218:10)
    at C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:233:3
    at runSyncOrAsync (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:130:11)
    at iterateNormalLoaders (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:229:2)
    at Array.<anonymous> (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:202:4)
    at Storage.finished (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:43:16)
    at provider (C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:79:9)
    at C:\Users\pauln\AppData\Roaming\npm\node_modules\webpack\node_modules\graceful-fs\graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:528:3)
 @ ./src/lib.js 12:14-38
 @ ./src/index.js
 @ multi (webpack)-dev-server/client?http://localhost:2000 ./src/index.js

ERROR in chunk main [entry]
bundle.js
Cannot read property 'replace' of undefined

My titles.json file looks like this:

{   
    "hello": "Bonjour",
    "goodbye": "Au Reviour"
}

What am I doing wrong?

EDIT Adding my webpack.config:

var webpack = require("webpack");

    module.exports = {
        entry: __dirname + "/src/index.js",
        output: {
            path: __dirname + "/dist/assets",
            filename: "bundle.js",
            publicPath: "assets",
        },
        devServer: {
            inline: true,
            contentBase: __dirname + '/dist',
            port: 2000
        },
        module: {
            rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                enforce: "pre",
                loader: "babel-loader",
                query: {
                    presets: ["latest", "stage-0", "react"]
                }

            },

            {
                test: /\.json$/,
                exclude: /(node_modules)/,
                loader: "json-loader"       
            }

            ]
        }
    }

Edit 2 Adding lib.js file and index.js file

lib.js

 import React from 'react'
    import text from './titles.json'

    export const hello = (
        <h1 id='title'
            className='header'
            style={{backgroundColor: 'purple', color: 'yellow'}}>
            {text.hello}
            </h1>
            )

    export const goodbye = (
        <h1 id='title'
            className='header'
            style={{backgroundColor: 'yellow', color: 'purple'}}>
            {text.goodbye}
            </h1>
        )       

Index.js

import React from 'react'
import { render } from 'react-dom'
import { hello, goodbye } from './lib'

render(
    <div>
       { hello }
        { goodbye }
    </div>,
     document.getElementById('react-container')
     )
9
  • What does your webpack config look like? Commented Mar 22, 2018 at 19:36
  • Updated with my webpack.config file. Commented Mar 22, 2018 at 19:42
  • can you show where you're trying to import this json file? Commented Mar 22, 2018 at 19:49
  • Yes. I do have the .babelrc file. That was one of the earlier videos. Commented Mar 22, 2018 at 19:56
  • inside your lib.js add import Text from '!json!./titles'; notice extra !json! at the start Commented Mar 22, 2018 at 19:57

5 Answers 5

83

I had the same issue but when I check the json-loader documentation here I found out that json-loader isn't necessary anymore in webpack version greater than or equal to 2.0. So you could remove the json-loader from your webpack config file and everything should work assuming you import your json file appropriately import text from './titles.json'

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

1 Comment

The json-loader documentation url is not valid anymore.
3

json-loader is not required anymore:

⚠️ Since webpack >= v2.0.0, importing of JSON files will work by default. You might still want to use this if you use a custom file extension.

Docs: https://webpack.js.org/migrate/3/#json-loader-is-not-required-anymore

Comments

1

I had the same issue and resolved it by ensuring no spacing on key and value on the JSON text

Comments

1

Had a similar caused by invalid data in JSON (using it as .jsonc and having a comment)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Another way to solve this is to modify your package.json file to add a browser section:

"browser": {
  "colors": false,
  "events": "eventemitter3",
  "checkForModuleDuplicates": false,
  "url": "./shims/url",      
  "tracer": false
}

Webpack will respect this section and ignore anything set to : false or otherwise load the corresponding module from the value of the key:value pair.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.