Richard
Richard

Reputation: 8945

React - Relative imports outside of src/ are not supported

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.

Upvotes: 6

Views: 19346

Answers (1)

r_batra
r_batra

Reputation: 410

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' 

Upvotes: 2

Related Questions