0

I have a react project where I am trying to import some data from a json file that sits outsite of the project.

I am attempting to import it like:

 const env = require('./../../env.json');

I get the following error:

Module not found: Error: You attempted to import ./../../env.json which falls outside of the project src/ directory. Relative imports outside of src/ are not supported. [1] You can either move it inside src/, or add a symlink to it from project's node_modules/.

I don't fully understand how it is possible to get data from outside of the project. I attempted using dotenv but had no luck :/

How is it possible to create the symlink?

Also, if the symlink is created locally, when I deploy to the server will the symlink still work? Is it saved in the project or would I need to add symlinks again on the server?

Any help on how to get access to data from outside my react project would be appreciated.

I have a project where I have multiple react applications that need to use the same json data.

I have the following directory sturcture:

-Project Root

--server.js

--env.json

--react-app-1

--react-app-2

lky
  • 1,081
  • 3
  • 15
  • 31
  • here the answer to your question https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory – Murad Sep 01 '22 at 18:31

1 Answers1

0

but for env variables you need to create .env file, not a JSON, and then you can use them with process.env.YOUR_ENV_VARIABLE_NAME without imports. Also don’t forget that for react app u should name your env variables started with REACT_APP_…, otherwise it will not work

Murad
  • 3
  • 3
  • I'm trying to use JSON as a work around for not being able to use dotenv within a react application? I receive ERROR in ./node_modules/dotenv/lib/main.js Module not found: Error: Can't resolve 'fs' and Error: Can't resolve 'os' – lky Sep 01 '22 at 20:26