Search code examples
javascriptreactjsecmascript-6

ReactJS import component outside src/ directory


I have two react apps(A-app, B-app). I need to import one component from A-app to B-app. But when I try to do this I see this error:

./src/App.js Module not found: You attempted to import
../../src/components/Dashboard/container which falls outside of the project
src/ directory. Relative imports outside of src/ are not supported. You can
either move it inside src/, or add a symlink to it from project's node_modules/.

I tried to do symlink on this component in B-app node_modules. But it didn't work.

Also I tried to create .env file in the root project directory and put this NODE_PATH=src/ in file. But this didn't work either.

How can I fix this?


Solution

  • Got to your A-app node_modules folder and run the following

    ln -s ../../../src/components/Dashboard ./app-b-dashboard
    

    After creating the following symbolic link in your node_modules folder you can import in you A-app

    import Dashboard from 'app-b-dashboard/container'
    

    The names might be different depending on the specific layout of your project. This is my best guess based on the info you provided.