Skip to content Skip to sidebar Skip to footer

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 tried to do this I have seen this error. ./src/App.js Module not found: You atte

Solution 1:

I stumbled on this post while trying to solve a similar issue. I think the solution might be relevant so I'm posting it here.

As of NPM 2.0 you can declare local dependencies in package.json. This allows you to maintain local modules in a folder outside of src/ and have them copied to node_modules during npm install.

Place your module anywhere outside of src/, for example:

./packages/app-b-dashboard

Declare a local dependency in package.json using the file: prefix:

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

Run

npm install

Now you can import it in your A-app

importDashboardfrom'app-b-dashboard/container'

Solution 2:

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

importDashboardfrom'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.

Solution 3:

are you using create react app ? if yes, you need to move your module into your src directory. This is special restriction added by developers of create-react-app . mentioned here

If moving the code is not an option for you, there are 2 solutions.

  • make the component as a module and npm install it like a private package
  • There is a workaround here

Solution 4:

I removed my node_module and package-lock.json files. and reinstall npm using npm install resolved my errors. I dont know this is a good way to do it. but its worked for me.

Post a Comment for "Reactjs Import Component Outside Src/ Directory"