问题
I built a Component that I want to use in separate projects. What is the simplest best way to reuse this Component across projects using NPM?
回答1:
You haven't indicated whether you wish to keep this component private, so I'm going to answer two ways:
If you are going to open-source your component
This is easiest: publish your component(s) as an npm
package (see the docs here). Then install your package just like any other in the projects where you will be using your components.
If you wish to keep it private
You have several options here:
pay for a private modules on
npm
(docs here). If you go this route, you can install your package just like any other.Use
npm link
(docs) to create a link from the project(s) to the component package located on your local storage. This works but is kind of tricky to set up. The benefit of this is that if you rebuild your component, all projects with a link to the component will see those changes immediately.Use
npm install
to install from your (private) Github or other repository. This works similarly to the normalnpm install
and is a little easier to use thannpm link
. The downside is that any changes to the component need to be pushed to the remote repository and then the project(s) using the component need to be updated.
来源:https://stackoverflow.com/questions/46148261/how-to-share-react-components-between-projects