【问题标题】:Mern Stack Application Deployment on Heroku (remote: npm ERR! Failed at the server@1.0.0 heroku-postbuild script.)Heroku 上的 Mern Stack 应用程序部署(远程:npm ERR!在 server@1.0.0 heroku-postbuild 脚本中失败。)
【发布时间】:2021-08-26 20:33:07
【问题描述】:

我正在尝试在 Heroku 上部署 MERN 堆栈应用程序,但在部署时遇到了一些问题,如下所示

 npm ERR! code ENOENT
remote: npm ERR! syscall open
remote: npm ERR! path /tmp/build_9fd3be63/client/package.json
remote: npm ERR! errno -2
remote: npm ERR! enoent ENOENT: no such file or directory, open '/tmp/build_9fd3be63/client/package.json'
remote: npm ERR! enoent This is related to npm not being able to find a file.
remote: npm ERR! enoent
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR!     /tmp/npmcache.mlfBJ/_logs/2021-05-03T03_26_07_032Z-debug.log
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! errno 254
remote: npm ERR! server@1.0.0 heroku-postbuild: `NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client`
remote: npm ERR! Exit status 254
remote: npm ERR!
remote: npm ERR! Failed at the server@1.0.0 heroku-postbuild script.
remote: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
remote:
remote: npm ERR! A complete log of this run can be found in:
remote: npm ERR!     /tmp/npmcache.mlfBJ/_logs/2021-05-03T03_26_07_059Z-debug.log
服务器的

Package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "engines": {
    "node": "14.x"
  },
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  },
  "author": "Umar24129",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mongodb": "^3.6.6"
  }
}

客户端的

Package.json

{
  "name": "react-task-tracker",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.12.0",
    "@testing-library/react": "^11.2.6",
    "@testing-library/user-event": "^12.8.3",
    "json-server": "^0.16.3",
    "react": "^17.0.2",
    "react-datetime-picker": "^3.2.1",
    "react-dom": "^17.0.2",
    "react-icons": "^4.2.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

文件系统(文件夹层次结构)截图如下。

我对此进行了很多研究,至少在 StackOverflow 或 Github 上找不到合适的解决方案。 请帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: node.js reactjs git express heroku


    【解决方案1】:

    问题是 npm 找不到你客户端的 package.json。 “heroku-postbuild”脚本失败,因为它必须安装和构建客户端。如果您的客户端位于另一个文件夹中,我建议将 --prefix 标记更改为以下内容:

    {
      "name": "server",
      "version": "1.0.0",
      "description": "",
      "engines": {
        "node": "14.x"
      },
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node index.js",
        "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false cd .. && cd client_dir && npm install && npm run build"
      },
      "author": "Umar24129",
      "license": "ISC",
      "dependencies": {
        "cors": "^2.8.5",
        "express": "^4.17.1",
        "mongodb": "^3.6.6"
      }
    }
    
    

    所以基本上你 cd 出服务器目录(假设你的服务器 package.json 在根目录中的服务器目录中) cd 进入客户端目录,然后运行 ​​npm install 和 build,假设你的客户端 package.json 是一个文件夹在根目录中命名为client_dir

    其他信息

    请在您的本地机器上运行 postbuild 脚本,看看它是否运行正常,如果没有,请发布日志文件,它将在命令行中指定位置。

    【讨论】:

    • 我在问题中添加了 Folder 层次结构。并且 Package.json 存在并且位于 Client 文件夹中。
    • 嗯,那么我不太确定是什么导致了这种行为,你能在运行 postbuild 脚本的机器上重新创建它吗?
    • 我做了好几次。我还尝试使用许多其他不同的方法部署相同的项目,但即使它在本地主机上构建和非构建版本都可以正常工作,它也无法工作。
    • 尝试在脚本或 Heroku 配置变量中设置“NPM_CONFIG_PRODUCTION=TRUE”
    【解决方案2】:

    我的问题是 Node 和 NPM 版本不正确。在教程中,我在看,NODE 有版本 8.something 和 npm 5.something。我将其更改为本地计算机上的版本:

      "engines": {
        "node": "10.19.0",
        "npm": "6.14.0"
      },
    

    应用部署成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 2022-09-26
      • 2021-09-02
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多