Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: How to run "react-scripts start" with a different root?

Tags:

npm

reactjs

For special reasons I want to share the package.json file between two folders web (the react app) and mobile:

▸ mobile/
▸ node_modules/
▾ web/
  ▸ public/
  ▸ src/
    README.md
  package-lock.json
  package.json
  yarn.lock

In my package.json file I've added this: "web-start": "react-scripts start", under scripts. However, when I run it in the root folder (/Users/edmund/Documents/src/banana-client) I get this:

➜  banana-client git:(master) ✗ yarn web-start
yarn web-start v0.24.6
$ react-scripts start web
Could not find a required file.
  Name: index.html
  Searched in: /Users/edmund/Documents/src/banana-client/public
error Command failed with exit code 1.

Is there a way I can add a root directory?

like image 364
bigpotato Avatar asked Jun 14 '17 06:06

bigpotato


People also ask

How do I start a react project on a specific port?

To change the default port for a create-react-app project, update the start command in your package. json file to specify the port, e.g. "PORT=3456 react-scripts start" on macOS and Linux and "set PORT=3456 && react-scripts start" on Windows.

How do I change the path in react project?

How can I change ReactJS project(folder) name, simply rename the folder name from routing (folder) to react-routing-example (folder).

What does react-scripts start run?

react-scripts are simply scripts to run the build tools required to transform React JSX syntax into plain JavaScript programmatically. When you run one of the scripts, the /bin/react-scripts. js will be executed to start the process. This script will look into the arguments that you passed into the call.

Can react-scripts be a dev dependency?

According to NPM dependencies definition, the build dependency, react-scripts , should be a devDependency . However, it is in the dependencies section along with react and react-dom . In fact, react-scripts was a devDependency . For some practical reason, Facebook made it a dependency since react-scripts 1.0.


2 Answers

For this particular use case, I would recommend you to go ahead with Yarn workspaces.

Using Yarn workspaces, you can have multiple projects inside a single repository with a main package.json file at root level that projects share and project level package.json that they don't share and use individually.

Running yarn install will install all your dependencies at the root level, which is configurable if you don't want some packages to install at root.

You can have scripts in root that can help you run internal projects in that workspace.

So your final project structure will look something like this:

- project_root
  - web
    - node_modules
    - package.json
  - mobile
    - node_modules
    - package.json

  - node_modules
  - package.json

Learn more about Yarn workspaces here: https://yarnpkg.com/lang/en/docs/workspaces/

Look at this example here to understand it's basic usage: https://github.com/pedronauck/yarn-workspaces-example

like image 180
Dangling Cruze Avatar answered Oct 17 '22 08:10

Dangling Cruze


I'd agree that Yarn workspaces may help you accomplish what you are after. Otherwise, you will have to eject as the configuration for the appIndexJs (set to /src/index.js) is set in config/paths.js in the react-scripts package. Once ejected, you can modify the config/paths.js variable appIndexJs to accommodate a varying appIndex.

like image 27
user2372736 Avatar answered Oct 17 '22 06:10

user2372736