【问题标题】:Error: Cannot read property '0' of undefined while deploying on Heroku错误:在 Heroku 上部署时无法读取未定义的属性“0”
【发布时间】:2021-03-28 08:27:05
【问题描述】:

我开发了一个 Angular 10 项目并准备好我的 package.json 文件以部署在 Heroku 上。可以看到package.json:

{
  "name": "rehabsoft-frontend",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "http-server-spa dist/rehabsoft-frontend index.html $PORT",
    "heroku-postbuild": "ng build --prod && npm install -g http-server-spa",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build-themes": "devextreme build"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~10.0.11",
    "@angular/cdk": "^10.2.7",
    "@angular/cli": "~10.0.7",
    "@angular/common": "~10.0.11",
    "@angular/compiler": "~10.0.11",
    "@angular/compiler-cli": "~10.0.11",
    "@angular/core": "~10.0.11",
    "@angular/forms": "~10.0.11",
    "@angular/platform-browser": "~10.0.11",
    "@angular/platform-browser-dynamic": "~10.0.11",
    "@angular/router": "~10.0.11",
    "@fortawesome/fontawesome-free": "^5.15.1",
    "@types/chart.js": "^2.9.27",
    "angular-bootstrap-md": "^10.0.0",
    "animate.css": "^4.1.1",
    "chart.js": "^2.5.0",
    "compression": "^1.7.4",
    "devextreme": "20.1.7",
    "devextreme-angular": "20.1.7",
    "devextreme-schematics": "^1.2.4",
    "express": "^4.17.1",
    "hammerjs": "^2.0.8",
    "ngx-cookie-service": "^10.1.1",
    "rxjs": "~6.5.5",
    "sweetalert2": "^10.10.3",
    "tslib": "^2.0.0",
    "typescript": "~3.9.5",
    "video.js": "^7.10.2",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.1100.3",
    "@angular/cli": "~10.0.7",
    "@angular/compiler-cli": "~10.0.11",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "devextreme-cli": "latest",
    "devextreme-intl": "^19.1.8",
    "devextreme-themebuilder": "20.1.7",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.5"
  },
  "engines": {
    "node": "14.15.5",
    "npm": "6.14.9"
  }
}

推送到 Heroku 后,它给了我这个错误:

- Generating browser application bundles...
✔ Browser application bundle generation complete.
Error: Cannot read property '0' of undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! rehabsoft-frontend@0.0.0 heroku-postbuild: `ng build --prod && npm install -g http-server-spa`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the rehabsoft-frontend@0.0.0 heroku-postbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /tmp/npmcache.E6zzA/_logs/2021-03-26T20_34_47_429Z-debug.log
-----> Build failed
       
       We're sorry this build is failing! You can troubleshoot common issues here:
       https://devcenter.heroku.com/articles/troubleshooting-node-deploys
       
       If you're stuck, please submit a ticket so we can help:
       https://help.heroku.com/
       
       Love,
       Heroku
       
 !     Push rejected, failed to compile Node.js app.
 !     Push failed

我尝试在本地运行ng build --prod && npm install -g http-server-spahttp-server-spa dist/rehabsoft-frontend index.html $PORT 命令,一切正常。我知道Cannot read property '0' of undefined 错误主要发生在代码中出现未定义的数组元素时。但是我已经在本地测试了所有内容,并且效果很好。有什么我没有注意到的错误点吗?

编辑:
我还运行heroku config:set NPM_CONFIG_LOGLEVEL=verbose 命令来查看更详细的日志输出。运行此命令后,您可以看到构建输出:

Compiling @angular/cdk/keycodes : es2015 as esm2015
Compiling @angular/cdk/observers : es2015 as esm2015
Compiling @angular/cdk/a11y : es2015 as esm2015
Compiling @angular/router : es2015 as esm2015
Compiling angular-bootstrap-md : es2015 as esm2015
- Generating browser application bundles...
✔ Browser application bundle generation complete.
Error: Cannot read property '0' of undefined
npm verb lifecycle rehabsoft-frontend@0.0.0~heroku-postbuild: unsafe-perm in lifecycle true
npm verb lifecycle rehabsoft-frontend@0.0.0~heroku-postbuild: PATH: /tmp/build_cfc9b878/.heroku/node/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/tmp/build_cfc9b878/node_modules/.bin:/tmp/build_cfc9b878/.heroku/node/bin:/tmp/build_cfc9b878/.heroku/yarn/bin:/usr/local/bin:/usr/bin:/bin
npm verb lifecycle rehabsoft-frontend@0.0.0~heroku-postbuild: CWD: /tmp/build_cfc9b878
npm info lifecycle rehabsoft-frontend@0.0.0~heroku-postbuild: Failed to exec heroku-postbuild script
npm verb stack Error: rehabsoft-frontend@0.0.0 heroku-postbuild: `ng build --prod && npm install -g http-server-spa`
npm verb stack Exit status 1
npm verb stack     at EventEmitter.<anonymous> (/tmp/build_cfc9b878/.heroku/node/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
npm verb stack     at EventEmitter.emit (events.js:315:20)
npm verb stack     at ChildProcess.<anonymous> (/tmp/build_cfc9b878/.heroku/node/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
npm verb stack     at ChildProcess.emit (events.js:315:20)
npm verb stack     at maybeClose (internal/child_process.js:1048:16)
npm verb stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
npm verb pkgid rehabsoft-frontend@0.0.0
npm verb cwd /tmp/build_cfc9b878
npm verb Linux 4.4.0-1086-aws
npm verb argv "/tmp/build_cfc9b878/.heroku/node/bin/node" "/tmp/build_cfc9b878/.heroku/node/bin/npm" "run" "heroku-postbuild" "--if-present"
npm verb node v14.15.5
npm verb npm  v6.14.9
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! rehabsoft-frontend@0.0.0 heroku-postbuild: `ng build --prod && npm install -g http-server-spa`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the rehabsoft-frontend@0.0.0 heroku-postbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm verb exit [ 1, true ]
npm timing npm Completed in 326009ms
npm ERR! A complete log of this run can be found in:
npm ERR!     /tmp/npmcache.WBTLI/_logs/2021-03-27T09_47_12_360Z-debug.log
-----> Build failed
       
       We're sorry this build is failing! You can troubleshoot common issues here:
       https://devcenter.heroku.com/articles/troubleshooting-node-deploys
       
       If you're stuck, please submit a ticket so we can help:
       https://help.heroku.com/
       
       Love,
       Heroku
       
 !     Push rejected, failed to compile Node.js app.
 !     Push failed

【问题讨论】:

    标签: node.js angular heroku


    【解决方案1】:

    在部署前端应用程序时,我发现实际上netlify 更易于使用,您是否尝试过,因为您只需要这样做

    npm install netlify-cli -g
    netlify deploy
    

    而且它也是免费的,没有任何意外情况,例如free dyno hours,另一种选择是 Github pages

    【讨论】:

    • 谢谢,我也试试这个平台。
    【解决方案2】:

    我找到了这个问题的原因。我在 Windows 10 上开发了该项目。当我在 Ubuntu 系统上运行该应用程序时,我遇到了一些在我的 Windows 系统上没有发生的编译错误。我不知道为什么我在 Windows 上没有收到这些错误。如果有人遇到这个问题,你应该先在 Ubuntu 上测试你的应用程序,看看你的应用程序是否真的可以工作。

    【讨论】:

      猜你喜欢
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      相关资源
      最近更新 更多