简体   繁体   English

NPM 与 React + Node + 并发运行构建如何?

[英]NPM run build with React + Node + concurrently How to?

I have spend hours trying to figure this out any advice is welcome.我花了几个小时试图弄清楚这一点,欢迎提出任何建议。 The objective here is to assemble a postbuild script that will work on a nodeJS app running a react client.这里的目标是组装一个 postbuild 脚本,该脚本将在运行 react 客户端的 nodeJS 应用程序上运行。

React is on post 3000 and node is on 5000. So it requires the concurrently library. React 在 3000 之后,node 在 5000 上。所以它需要并发库。 Below are two attempts do-postbuild and heroku-postbuild (both fail).下面是两次尝试do-postbuildheroku-postbuild (都失败)。

  "scripts": {

    "server": "nodemon server.js --ignore client",
    "client": "npm start --prefix ../client",
    "dev": "concurrently \"npm run server\" \"npm run client\" ",
    "do-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix && npm run build --prefix client",
    "heroku-postbuild": "cd ../client && npm install && npm install --only=dev --no-shrinkwrap && npm run build" 
  },

folder structure文件夹结构

client
server
   |_package.json (above)
   |_server.js

npm run dev - WORKS perfectly npm run dev - 完美运行

When I attempt npm run heroku-postbuild it yields the following:当我尝试npm run heroku-postbuild它会产生以下结果:

npm ERR! errno 1
npm ERR! ver1.02@1.0.0 heroku-postbuild: `cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build`
npm ERR! Exit status 1

When attempting to write npm run do-postbuild it throws an error like it is searching for client in the server folder当尝试编写npm run do-postbuild它会抛出一个错误,就像在服务器文件夹中搜索客户端一样

npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/Users/sites/server/client/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

This is not a HEROKU solution it is for general UBUNTU server with root access.这不是 HEROKU 解决方案,它适用于具有 root 访问权限的通用 UBUNTU 服务器。

Solution here is you don't have to dockerize the app as a bundle (client and server together).这里的解决方案是您不必将应用程序打包为一个包(客户端和服务器在一起)。

What worked for me was to treat the client and server as two different apps.对我有用的是将客户端和服务器视为两个不同的应用程序。

Client side:客户端:

  • npm run build locally from the same folder as contains your package.json file npm run build从包含 package.json 文件的同一文件夹本地npm run build
  • then post the app build folder as very straight forward client side app with HTML CSS Javascript然后使用 HTML CSS Javascript 将应用程序build folder为非常简单的客户端应用程序

Server side:服务器端:

  • upload the server files (not including node_modules folder)上传server文件(不包括 node_modules 文件夹)
  • run npm i (from the folder with the package.json file)运行npm i (从包含 package.json 文件的文件夹中)
  • I set up reverse proxy to map the port to a specific location on the server for the react to reach it我设置了反向代理以将端口映射到服务器上的特定位置,以便反应到达它
  • set up cron job to start server side (and check periodically to ensure it is running)设置 cron 作业以启动服务器端(并定期检查以确保它正在运行)

Thats it - works perfect.就是这样 - 工作完美。

将此添加到您的脚本中

 "client-install": "npm install --prefix client",

Please add below Scripts and try, it will work 100%.请添加以下脚本并尝试,它将 100% 工作。

 "scripts": { 

 "client-install": "npm install --prefix client",
      "start": "node server.js",
      "server": "nodemon server.js",
      "client": "npm start --prefix client",
      "dev": "concurrently \"npm run server\" \"npm run client\"",
      "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
    } 

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

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