NPM 运行 build with React + Node + concurrently 如何?

NPM run build with React + Node + concurrently How to?

我花了几个小时试图解决这个问题,欢迎任何建议。这里的 objective 是 assemble 一个 post 将在 nodeJS 应用程序上运行的构建脚本 运行 一个反应客户端。

React 在 post 3000 上,node 在 5000 上。所以它需要 concurrent 库。以下是两次尝试 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" 
  },

文件夹结构

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

npm run dev - WORKS perfectly

当我尝试 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

当尝试写入 npm run do-postbuild 时它抛出一个错误,就像它在 server 文件夹中搜索 client

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 

这不是 HEROKU 解决方案,它适用于具有根访问权限的一般 UBUNTU 服务器。

这里的解决方案是您不必将应用程序打包为一个包(客户端和服务器在一起)。

对我有用的是将客户端和服务器视为两个不同的应用程序。

客户端:

  • npm run build 本地来自包含您的 package.json 文件
  • 的同一文件夹
  • 然后 post 应用程序 build folder 作为非常直接的客户端应用程序 HTML CSS Javascript

服务器端:

  • 上传 server 个文件(不包括 node_modules 个文件夹)
  • 运行 npm i(来自包含 package.json 文件的文件夹)
  • 我设置了反向代理以将端口映射到服务器上的特定位置,以便 React 能够访问它
  • 设置 cron 作业以启动服务器端(并定期检查以确保它是 运行ning)

就是这样 - 完美。

将此添加到您的脚本中

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

请添加以下脚本并尝试,它将 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"
    }