Categories
React Answers

How to import component outside src/ directory with React?

Spread the love

To import component outside src/ directory with React, we can declare a local dependency on package.json.

For instance, we write

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

to declare the app-b-dashboard dependency in the dependencies section of package.json.

Then we run

npm install

to install the dependencies.

And then we add

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

to import the app-b-dashboard dependency which is installed in node_modules.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *