question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how best to incorporate an existing package created with create-react-app

See original GitHub issue

I have been working on a package that has been created with create-react-app.

I have now come across lerna and would like be able to use lerna to link the local pacakges together without using npm link.

How can I refactor my application into a structure that would work with lerna.

Is it even worth it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
pclements12commented, Jul 10, 2017

I’ve made a successful version of a create-react-app (with Redux) using lerna, but it’s a little complicated. I wouldn’t recommend the approach unless you truly need it.

This is how I’ve set it up:

Module Structure

  • App module
  • Other modules

App module

The main app module contains many of the root level files that are essential to a React application:

  • index.js (The main entry point of the React application that mounts it to the DOM)
  • main-router.jsx (Hook up all of the routes from each package)
  • reducers.js (combine all the reducers)
  • store.js (Create the Redux store with the reducers and any middleware required)

Other modules

All modules follow this componetized pattern:

src/
  index.js
  routes/
    {route-name}/
    index.js
  components/
    {component-name}/
    index.js
  reducers/
    {reducer-name}/
    index.js
    selectors.js

Within each root level folder, an index.js file should exist that collects all of the exports that should be exposed to other modules so that the root index.js can succinctly expose each of these for the entire module.

The root index.js should expose (I’d love a more flat way to express this, especially for components):

components,
selectors,
reducers,
logic,
route,
theme

To run the app, you need a few things:

  • Run lerna bootstrap to install all deps and link packages
  • Run babel with the --watch flag for each “Other” package (for live edits)
  • Run the regular create react app start script to start a webpack dev server with hot reloading

Hope that helps or at least tells you it can be done!

Edit: Composing the routes can be a bit tricky in the “app” module. I had to use this technique to get it to work:

Module Route definition:

const Module1Routes = () => (
  <Route path="/my-route">
    <IndexRoute component={header} />
    <Route path="/my-route/page" component={page} />
    <Route path="/my-route/page2" component={page2} />
  </Route>
);

App main-router.jsx:

 <Router>
        {Module1Routes()}
        {Module2Routes()}
</Router>
2reactions
ctrlplusbcommented, Mar 6, 2018

FYI, for those in the interest. All works well with straight up yarn workspaces. Although you may need to firstly create your create-react-app instance outside of your workspace and then copy the result into your workspace and run the yarn install command again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deployment | Create React App
Create a file called .env.staging · Install env-cmd. $ npm install env-cmd --save $ # or $ yarn add env-cmd · Add a...
Read more >
Create a New React App
Create React App is a comfortable environment for learning React, and is the best way to start building a new single-page application in...
Read more >
How To Set Up a React Project with Create React App
To install the base project, run the following command: npx create-react-app digital-ocean-tutorial.
Read more >
Switch over to Create React App on Existing Project?
Copy your src to a folder outside your repo; Clean your repo git rm -rf; Perform the above steps (create react app, install...
Read more >
How to Build a React App Using Create-React-App - Radixweb
You need to install Node.js and NPM on your desktop. You can download Node at nodejs.org. Make sure to have at least Node...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found