【问题标题】:React: How to run "react-scripts start" with a different root?React:如何使用不同的根运行“react-scripts start”?
【发布时间】:2017-11-16 03:56:32
【问题描述】:

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

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

在我的 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.

有没有办法添加根目录?

【问题讨论】:

  • 你使用弹出的 create-react-app 吗? react-scripts 是 CRA 的一部分...

标签: reactjs npm


【解决方案1】:

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

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

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

您可以在根目录中使用脚本来帮助您在该工作区中运行内部项目。

因此您的最终项目结构将如下所示:

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

  - node_modules
  - package.json

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

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

【讨论】:

    【解决方案2】:

    我同意 Yarn 工作区可以帮助您完成您所追求的目标。否则,您将不得不弹出,因为appIndexJs 的配置(设置为/src/index.js)在react-scripts 包中的config/paths.js 中设置。弹出后,您可以修改 config/paths.js 变量 appIndexJs 以适应不同的 appIndex。

    【讨论】:

      【解决方案3】:

      添加此解决方法为我解决了同样的问题:

       "web-start": "cd web && react-scripts start"
      

      【讨论】:

        猜你喜欢
        • 2018-01-14
        • 2017-11-07
        • 2017-03-25
        • 2020-06-05
        • 2019-11-19
        • 2022-12-14
        • 2020-03-11
        • 1970-01-01
        • 2020-11-25
        相关资源
        最近更新 更多