5

I am using React, and am trying to access a resource that is not in the src directory, this results in an error.

Question

Is it possible to access a .js (i.e. index.js) file that is outside the src directory?

I have the following directory structure:

- client
     - src
          - App.js
- data
     - index.js

index.js

const ShiftList = [
   {... data ...}
]

module.exports = { ShiftList }

App.js

import { ShiftList } from '../../data/index.js';

I get the following error:

./src/App.js Module not found: You attempted to import ../../data/index which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.

Richard
  • 8,193
  • 28
  • 107
  • 228
  • Did you create the project using create-react-app? https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory – krmckone Oct 23 '19 at 14:26
  • Possible duplicate of [The create-react-app imports restriction outside of src directory](https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory) – Andrii Golubenko Oct 23 '19 at 14:31
  • @krmckone, thanks for the reply. No I did not. It is something I have inherited. – Richard Oct 23 '19 at 14:31
  • Does this answer your question? [ReactJS import component outside src/ directory](https://stackoverflow.com/questions/49705170/reactjs-import-component-outside-src-directory) – StudioTime Nov 21 '20 at 11:31

1 Answers1

2

For this you might do the following in package.json

"dependencies": {
  "app-b-dashboard": "file:./data/index"
}

and then you should be able to import as

import Dashboard from 'app-b-dashboard/container' 
r_batra
  • 400
  • 3
  • 14