import a Json file in a React Component

前端 未结 1 674
青春惊慌失措
青春惊慌失措 2020-12-11 07:51

I\'m trying to load languages.json file in a React component. I\'m getting the following error at the very first step when I want to import the json file. Here is the error:

相关标签:
1条回答
  • 2020-12-11 08:08

    azium is correct that a loader is needed, but here's the configuration for good measure:

    npm command

    > npm install json-loader --save-dev
    

    webpack.config.js

     module.exports = {
        ....
        resolve: {
            extensions: ['', '.js', '.jsx', '.json']
        },
        ...
       module: {
           ...
                {
                    test: /\.json$/,
                    loader: 'json'
                }
           ...
       }
    }
    

    By adding the json extension to resolve, you won't need to specify it in your import statement.

    0 讨论(0)
提交回复
热议问题