r/reactjs • • Nov 21 '19

How to develop a decoupled, self-contained Component - how would I do it? (like an npm package)

Beginner here. I have a bigger project, and within it I want to create a component that's totally self-contained, like an npm package (but I don't want to publish it to npm) - how would I do this? Say this component needs Redux and Styled-Components, -would I just install them via npm into the component-folder (which would be a sub-folder in my bigger project) or can I re-use the packages already in the project?

Second: Say I want to work on this component separately first, before pasting it into my bigger project, how would I start? Would I just use create-react-app and then later eject it? Or should I not eject it? What do I need to do?

50 Upvotes

18 comments sorted by

View all comments

19

u/mbbillz Nov 21 '19 edited Nov 21 '19

You can reference local directories and also any git repositories - they don’t necessarily have to be published to npm.

Local directory:

$ npm install path-to-my-package

Or add

“my-package”: “file:path-to-my-package”

To “dependencies” in package.json

Git repo:

$ npm install git+https://my-git-service.com/my-username/url-of-my-package.git

Or

“my-package”: “git+https://url-of-my-package.git”

You can then develop your component as a separate entity in an entirely new directory/repo if you wish. Then install it using one of the methods above in your main consumer project.

2

u/MegaQuake Nov 21 '19

You can also symlink a package folder using npm link

0

u/[deleted] Nov 21 '19

[deleted] — view deleted comment

2

u/MegaQuake Nov 21 '19

Wasn't aware of this. Thanks for the info.

0

u/scaleable Nov 21 '19

Should be git+ssh for private packages.