Import JSON file in React

前端 未结 11 1004
遥遥无期
遥遥无期 2020-11-28 06:05

I\'m new to React and I\'m trying to import a JSON DATA variable from an external file. I\'m getting the following error:

Cannot find mo

相关标签:
11条回答
  • 2020-11-28 06:46
    var langs={
      ar_AR:require('./locale/ar_AR.json'),
      cs_CZ:require('./locale/cs_CZ.json'),
      de_DE:require('./locale/de_DE.json'),
      el_GR:require('./locale/el_GR.json'),
      en_GB:require('./locale/en_GB.json'),
      es_ES:require('./locale/es_ES.json'),
      fr_FR:require('./locale/fr_FR.json'),
      hu_HU:require('./locale/hu_HU.json')
    }
    module.exports=langs;
    

    Require it in your module:

    let langs=require('./languages');
    

    regards

    0 讨论(0)
  • 2020-11-28 06:49

    This old chestnut...

    In short, you should be using require and letting node handle the parsing as part of the require call, not outsourcing it to a 3rd party module. You should also be taking care that your configs are bulletproof, which means you should check the returned data carefully.

    But for brevity's sake, consider the following example:

    For Example, let's say I have a config file 'admins.json' in the root of my app containing the following:

    admins.json
    [{
      "userName": "tech1337",
      "passSalted": "xxxxxxxxxxxx"
    }]
    

    Note the quoted keys, "userName", "passSalted"!

    I can do the following and get the data out of the file with ease.

    let admins = require('~/app/admins.json');
    console.log(admins[0].userName);
    

    Now the data is in and can be used as a regular (or array of) object.

    0 讨论(0)
  • 2020-11-28 06:50

    This worked well in React 16.11.0

    // in customData.js
    export const customData = {
      //json data here
      name: 'John Smith',
      imgURL: 'http://lorempixel.com/100/100/',
      hobbyList: ['coding', 'writing', 'skiing']
    }
    
    // in index.js
    import { customData } from './customData';
    
    // example usage later in index.js
    <p>{customData.name}</p>
    
    0 讨论(0)
  • 2020-11-28 06:55

    The solution that worked for me is that:- I moved my data.json file from src to public directory. Then used fetch API to fetch the file

    fetch('./data.json').then(response => {
          console.log(response);
          return response.json();
        }).then(data => {
          // Work with JSON data here
          console.log(data);
        }).catch(err => {
          // Do something for an error here
          console.log("Error Reading data " + err);
        });
    

    The problem was that after compiling react app the fetch request looks for the file at URL "http://localhost:3000/data.json" which is actually the public directory of my react app. But unfortunately while compiling react app data.json file is not moved from src to public directory. So we have to explicitly move data.json file from src to public directory.

    0 讨论(0)
  • 2020-11-28 06:58

    try with export default DATA or module.exports = DATA

    0 讨论(0)
  • 2020-11-28 07:00

    // rename the .json file to .js and keep in src folder

    Declare the json object as a variable

    var customData = {
       "key":"value"
    };
    

    Export it using module.exports

    module.exports = customData;

    From the component that needs it, make sure to back out two folders deep

    import customData from '../customData';

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