简体   繁体   English

Rails/React/Yarn:如何从应用程序的根目录运行“yarn start”

[英]Rails/React/Yarn: How can I run "yarn start" from the root of my application

I'm creating a rails application from scratch using the following blog which needs me to run rails s and then yarn start in the /client folder of the application (where all the JS and React components will live).我正在使用以下博客从头开始创建一个 rails 应用程序,它需要我运行rails s ,然后在应用程序的/client文件夹中yarn start (所有 JS 和 React 组件都将在其中运行)。

The /client/package.json file has some scripts configured to run a server that detects changes in my react components and reloads the components automatically: /client/package.json文件配置了一些脚本来运行服务器,该服务器检测我的 react 组件中的更改并自动重新加载组件:

client/package.json:客户端/package.json:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "proxy": "http://localhost:3001"
}

I use a program called hivemind which is a Procfile manager and I would like both the rails server process and the yarn process to run together in the same terminal.我使用一个名为hivemind的程序,它是一个Procfile管理器,我希望 rails 服务器进程和 yarn 进程在同一个终端中一起运行。

I'd like to do something like this in my Profile :我想在我的Profile做这样的事情:

server: bin/rails server
react:  yarn start

The problem is that Profile lives in the root of my application and I have to change directory into client and then run yarn start .问题是Profile位于我的应用程序的根目录中,我必须将目录更改为client ,然后运行yarn start

TL;DR :特尔;博士

Is there an option with yarn run that you can tell it to run from another folder or read a package.json file in another folder and have it run the script in that file? yarn run是否有一个选项可以告诉它从另一个文件夹运行或读取另一个文件夹中的package.json文件并让它运行该文件中的脚本?

You can write cmd arguments in Procfile您可以在Procfile编写cmd参数

try changing react command to尝试将反应命令更改为

react: cd client && yarn start

It will change the current directory and runs your script它将更改当前目录并运行您的脚本

When you run yarn [command] yarn looks for a file locally called package.json .当您运行yarn [command] yarn在本地查找名为package.json的文件。 You can add yarn commands inside the "scripts" section of this file.您可以在此文件的"scripts"部分中添加纱线命令。 In my own rails app root directory I added a "start" command that did what Sumanth's answer does but now I can just have在我自己的 rails 应用程序根目录中,我添加了一个"start"命令,该命令执行 Sumanth 的回答,但现在我可以拥有

react: yarn start

in my Procfile .在我的Procfile

Below is the edited version of my package.json file in my app's root path:以下是我的应用程序根路径中的package.json文件的编辑版本:

{
  "name": "create-repack-app",
  "version": "1.0.0",
  "scripts": {
    "build": "cd client && npm install --only=dev && npm install && npm run build && cd ..",
    "deploy": "cp -a client/build/. public/",
    "heroku-postbuild": "npm run build && npm run deploy && echo 'Client Built'",
    "start": "cd client && yarn start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM