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

时间:2017-06-14 06:41:40

标签: reactjs npm

出于特殊原因,我想在两个文件夹web(反应应用)和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.

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

3 个答案:

答案 0 :(得分:0)

对于此特定用例,建议您继续使用Yarn工作区。

使用Yarn工作区,您可以在一个存储库中拥有多个项目,这些项目的根级别为main 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

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

添加此替代方法可以为我解决相同的问题:

 "web-start": "cd web && react-scripts start"
相关问题