React:如何用不同的root运行"react-scripts start"?

big*_*ato 11 npm reactjs

出于特殊原因,我想在两个文件夹web(反应应用程序)之间共享package.json文件,并且mobile:

? mobile/
? node_modules/
? web/
  ? public/
  ? src/
    README.md
  package-lock.json
  package.json
  yarn.lock
Run Code Online (Sandbox Code Playgroud)

在我的package.json文件中,我添加了这个: "web-start": "react-scripts start",在脚本下.但是,当我在根文件夹(/Users/edmund/Documents/src/banana-client)中运行它时,我得到这个:

?  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.
Run Code Online (Sandbox Code Playgroud)

有没有办法可以添加根目录?

Dan*_*uze 2

对于这个特定的用例,我建议您继续使用 Yarn 工作区。

使用 Yarn 工作区,您可以在单个存储库中拥有多个项目,其中项目共享的根级别的主 package.json 文件和它们不共享和单独使用的项目级别的 package.json 文件。

运行yarn install将在根级别安装所有依赖项,如果您不希望某些软件包安装在根级别,则可以配置该依赖项。

您可以在根目录中包含脚本,以帮助您在该工作区中运行内部项目。

所以你的最终项目结构将如下所示:

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

  - node_modules
  - package.json
Run Code Online (Sandbox Code Playgroud)

在此处了解有关 Yarn 工作区的更多信息:https ://yarnpkg.com/lang/en/docs/workspaces/

请查看此处的示例以了解其基本用法: https ://github.com/pedronauck/yarn-workspaces-example