0

I have created react application using npx create-react-app. I have json files in public/strings folder.I need to access this json file from the reducer in src.I have code as below in src/reducers/lang_reducer.js

let resources =require("/strings/strings_" + language + ".json"); let englishStrings=require("/strings/strings_en.json"); 

It is throwing following error: ./src/reducers/languages_reducer.js Module not found: Can't resolve '/strings' in 'E:Code\src\reducers'

Is there any other way by which i can access json file which is in public folder from src ?

Thanks in advance :)

5
  • Sorry. You can post your answer here Commented Nov 30, 2018 at 12:46
  • Well, why did you delete it? Commented Nov 30, 2018 at 12:52
  • stackoverflow.com/questions/53519527/… Commented Nov 30, 2018 at 12:56
  • Check out require.context. Use it to include files in a directory, and then you require your language at runtime by using the embedded json files within webpack. Commented Nov 30, 2018 at 12:57
  • 1
    Could you please give an example of how to implement it for my current error? Commented Nov 30, 2018 at 13:03

1 Answer 1

0

create-react-app configures a lot of features automatically. This restricts you from accessing files which are not in src folder. One workaround for this is to eject and then overwrite the configuration. Other wise instead of using create-react-app you can do your webpack configuration on your own.

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

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.