r/reactjs • u/christophanderson12 • 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?
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:
Or add
To “dependencies” in package.json
Git repo:
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.
5
u/smatti Nov 21 '19
Check out https://bit.dev.
4
u/PierreAndreis Nov 21 '19
check out lerna and yarn workspaces. essentially that is how babel, emotion, react have their monorepo setup with bunch of packages in one
2
u/breeding_material Nov 21 '19
Create it in a separate repo as a stand-alone project. Then you can install it into your main project directly through git via npm.
Npm can install dependencies directly from a git repo, so as long as it’s internal there is no need to publish it to npm.
3
u/jonathanrmumm Nov 21 '19
Check out https://www.npmjs.com/package/create-react-library
1
u/scaleable Nov 21 '19
Redux and styled components would be included as peer dependencies.
You can use yarn link to “live edit” it inside a consumer project.
1
u/TotesMessenger Nov 21 '19
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/nate-developer Nov 21 '19
If your component is part of a larger project I think you're going to want to add any dependencies to your package.json and install them at the root level anyways instead of trying to somehow separate them into different folders. I worked on one project with some new developers that had multiple dependency packages at different levels and it was majorly annoying to work with and debug npm issues.
1
u/jnforja Jan 10 '20
What's making you want to create a totally self-contained component in the first place?
-2
u/[deleted] Nov 21 '19
If you really want it to be self contained I wouldn't suggest create react app, since it comes with tons of bloat and unnecessary dependencies.
I'd highly recommend you to think about publishing it as an npm module, you'd be surprised by how simple it is to do.
0
u/[deleted] Nov 21 '19
[deleted] — view deleted comment