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 :)

Prox4
  • 91
  • 1
  • 3
  • 13
  • Sorry. You can post your answer here – Prox4 Nov 30 '18 at 12:46
  • Well, why did you delete it? – Blue Nov 30 '18 at 12:52
  • https://stackoverflow.com/questions/53519527/how-to-access-json-file-in-src-folder-created-using-create-react-app?noredirect=1#comment93907715_53519527 – Prox4 Nov 30 '18 at 12:56
  • Check out [require.context](https://stackoverflow.com/questions/29421409/how-to-load-all-files-in-a-directory-using-webpack-without-require-statements). Use it to include files in a directory, and then you require your language at runtime by using the embedded json files within webpack. – Blue Nov 30 '18 at 12:57
  • 1
    Could you please give an example of how to implement it for my current error? – Prox4 Nov 30 '18 at 13:03

1 Answers1

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.

Kuncheria
  • 1,182
  • 9
  • 16