首页 > 解决方案 > 将 MERN 应用程序部署到 Heroku 时,如何解决“babel: not found”和“Cannot find module './common'?

问题描述

尝试在 Heroku 中部署 MERN 堆栈应用程序,并在日志中提到找不到 Babel 和模块 './common'

在尝试部署之前,我已经运行了 npm install,我已经删除了 Babel 的所有版本 6 实例以升级到版本 7,并且我也重新配置了构建脚本,但无济于事。

Package.json - 脚本:

"scripts": {
    "start": "npm run build && node dist/index.js",
    "serve": "node dist/index.js",
    "build": "babel ./src/index.js -d dist",
    "heroku-postbuild": "cd front-end && npm install --only=dev && npm install && npm run build"
  },

Package.json - 依赖项:

"dependencies": {
    "@babel/cli": "^7.6.0",
    "@babel/core": "^7.6.0",
    "@babel/preset-env": "^7.6.0",
    "@babel/register": "^7.6.0",
    "@elastic/elasticsearch": "^7.2.0",
    "aws-sdk": "^2.518.0",
    "aws4": "^1.8.0",
    "axios": "^0.19.0",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "cheerio": "^1.0.0-rc.3",
    "common": "^0.2.5",
    "connect-mongo": "^2.0.2",
    "cookie-parser": "^1.4.3",
    "cors": "^2.8.4",
    "dotenv": "^7.0.0",
    "elasticsearch": "^16.3.0",
    "express": "^4.16.3",
    "express-session": "^1.15.6",
    "express-validator": "^5.3.1",
    "helmet": "^3.18.0",
    "http-aws-es": "^6.0.0",
    "install": "^0.13.0",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.11",
    "mongoosastic": "^4.5.0",
    "mongoose": "^5.3.4",
    "morgan": "^1.9.1",
    "multer": "^1.4.1",
    "node-mailjet": "^3.3.1",
    "node-pre-gyp": "^0.13.0",
    "passport": "^0.4.0",
    "passport-custom": "^1.0.5",
    "passport-jwt": "^4.0.0",
    "passport.socketio": "^3.7.0",
    "request": "^2.88.0",
    "request-promise": "^4.2.4",
    "socket.io": "^2.2.0",
    "socket.io-redis": "^5.2.0",
    "stripe": "^6.25.1",
    "swagger": "^0.7.5",
    "swagger-ui-express": "^4.0.2",
    "sync-request": "^6.1.0",
    "validator": "^10.8.0",
    "winston": "^3.1.0",
    "xss": "^1.0.3"
  },
  "devDependencies": {
    "nodemon": "^1.18.4"
  }

Heroku 日志:

›   Warning: heroku update available from 7.26.2 to 7.29.0.
2019-09-06T21:25:37.376275+00:00 app[web.1]: > paymentPlatform@0.3.0 build /app
2019-09-06T21:25:37.376282+00:00 app[web.1]: > babel ./src/index.js -d dist
2019-09-06T21:25:37.376284+00:00 app[web.1]: 
2019-09-06T21:25:37.397001+00:00 app[web.1]: sh: 1: babel: not found
2019-09-06T21:25:37.404228+00:00 app[web.1]: npm ERR! file sh
2019-09-06T21:25:37.404233+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-09-06T21:25:37.404235+00:00 app[web.1]: npm ERR! errno ENOENT
2019-09-06T21:25:37.404238+00:00 app[web.1]: npm ERR! syscall spawn
2019-09-06T21:25:37.404242+00:00 app[web.1]: npm ERR! paymentPlatform@0.3.0 build: `babel ./src/index.js -d dist`
2019-09-06T21:25:37.404244+00:00 app[web.1]: npm ERR! spawn ENOENT
2019-09-06T21:25:37.404247+00:00 app[web.1]: npm ERR!
2019-09-06T21:25:37.407890+00:00 app[web.1]: npm ERR! Failed at the paymentPlatform@0.3.0 build script.
2019-09-06T21:25:37.408099+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-09-06T21:25:37.418872+00:00 app[web.1]: 
2019-09-06T21:25:37.419240+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-09-06T21:25:37.419654+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-09-06T21_25_37_410Z-debug.log
2019-09-06T21:25:37.439598+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-09-06T21:25:37.439920+00:00 app[web.1]: npm ERR! errno 1
2019-09-06T21:25:37.441347+00:00 app[web.1]: npm ERR! paymentPlatform@0.3.0 start: `npm run build && node dist/index.js`
2019-09-06T21:25:37.441475+00:00 app[web.1]: npm ERR! Exit status 1
2019-09-06T21:25:37.441652+00:00 app[web.1]: npm ERR!
2019-09-06T21:25:37.441800+00:00 app[web.1]: npm ERR! Failed at the paymentPlatform@0.3.0 start script.
2019-09-06T21:25:37.441910+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-09-06T21:25:37.452916+00:00 app[web.1]: 
2019-09-06T21:25:37.453949+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-09-06T21:25:37.455068+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-09-06T21_25_37_443Z-debug.log
2019-09-06T21:25:37.550220+00:00 heroku[web.1]: State changed from starting to crashed
2019-09-06T21:25:37.516263+00:00 heroku[web.1]: Process exited with status 1
2019-09-06T21:27:38.000000+00:00 app[api]: Build started by user ****.****@****.com
2019-09-06T21:31:22.176037+00:00 heroku[web.1]: State changed from crashed to starting
2019-09-06T21:31:22.014875+00:00 app[api]: Deploy 4dff7a9b by user ****.****@****.com
2019-09-06T21:31:22.014875+00:00 app[api]: Release v21 created by user ****.****@****.com
2019-09-06T21:31:29.000000+00:00 app[api]: Build succeeded
2019-09-06T21:31:52.195501+00:00 heroku[web.1]: Starting process with command `npm start`
2019-09-06T21:31:54.048390+00:00 app[web.1]: 
2019-09-06T21:31:54.048412+00:00 app[web.1]: > paymentPlatform@0.3.0 start /app
2019-09-06T21:31:54.048414+00:00 app[web.1]: > npm run build && node dist/index.js
2019-09-06T21:31:54.048416+00:00 app[web.1]: 
2019-09-06T21:31:54.590422+00:00 heroku[web.1]: State changed from starting to crashed
2019-09-06T21:31:54.367485+00:00 app[web.1]: 
2019-09-06T21:31:54.367522+00:00 app[web.1]: > paymentPlatform@0.3.0 build /app
2019-09-06T21:31:54.367532+00:00 app[web.1]: > babel ./src/index.js -d dist
2019-09-06T21:31:54.367534+00:00 app[web.1]: 
2019-09-06T21:31:54.481962+00:00 app[web.1]: internal/modules/cjs/loader.js:628
2019-09-06T21:31:54.481965+00:00 app[web.1]: throw err;
2019-09-06T21:31:54.481966+00:00 app[web.1]: ^
2019-09-06T21:31:54.481968+00:00 app[web.1]: 
2019-09-06T21:31:54.481970+00:00 app[web.1]: Error: Cannot find module './common'
2019-09-06T21:31:54.481971+00:00 app[web.1]: Require stack:
2019-09-06T21:31:54.481974+00:00 app[web.1]: - /app/node_modules/debug/src/node.js
2019-09-06T21:31:54.481975+00:00 app[web.1]: - /app/node_modules/debug/src/index.js
2019-09-06T21:31:54.481977+00:00 app[web.1]: - /app/node_modules/@babel/core/lib/config/files/configuration.js
2019-09-06T21:31:54.481978+00:00 app[web.1]: - /app/node_modules/@babel/core/lib/config/files/index.js
2019-09-06T21:31:54.481980+00:00 app[web.1]: - /app/node_modules/@babel/core/lib/index.js
2019-09-06T21:31:54.481981+00:00 app[web.1]: - /app/node_modules/@babel/cli/lib/babel/options.js
2019-09-06T21:31:54.481983+00:00 app[web.1]: - /app/node_modules/@babel/cli/lib/babel/index.js
2019-09-06T21:31:54.481984+00:00 app[web.1]: - /app/node_modules/@babel/cli/bin/babel.js
2019-09-06T21:31:54.481986+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:625:15)
2019-09-06T21:31:54.481987+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:527:27)
2019-09-06T21:31:54.481989+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:683:19)
2019-09-06T21:31:54.481990+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:16:16)
2019-09-06T21:31:54.481992+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/debug/src/node.js:236:18)
2019-09-06T21:31:54.481994+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:776:30)
2019-09-06T21:31:54.481995+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
2019-09-06T21:31:54.481997+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:643:32)
2019-09-06T21:31:54.481998+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:556:12)
2019-09-06T21:31:54.482000+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:683:19) {
2019-09-06T21:31:54.482001+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2019-09-06T21:31:54.482003+00:00 app[web.1]: requireStack: [
2019-09-06T21:31:54.482004+00:00 app[web.1]: '/app/node_modules/debug/src/node.js',
2019-09-06T21:31:54.482005+00:00 app[web.1]: '/app/node_modules/debug/src/index.js',
2019-09-06T21:31:54.482007+00:00 app[web.1]: '/app/node_modules/@babel/core/lib/config/files/configuration.js',
2019-09-06T21:31:54.482009+00:00 app[web.1]: '/app/node_modules/@babel/core/lib/config/files/index.js',
2019-09-06T21:31:54.482010+00:00 app[web.1]: '/app/node_modules/@babel/core/lib/index.js',
2019-09-06T21:31:54.482012+00:00 app[web.1]: '/app/node_modules/@babel/cli/lib/babel/options.js',
2019-09-06T21:31:54.482013+00:00 app[web.1]: '/app/node_modules/@babel/cli/lib/babel/index.js',
2019-09-06T21:31:54.482014+00:00 app[web.1]: '/app/node_modules/@babel/cli/bin/babel.js'
2019-09-06T21:31:54.482016+00:00 app[web.1]: ]
2019-09-06T21:31:54.482017+00:00 app[web.1]: }
2019-09-06T21:31:54.487548+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-09-06T21:31:54.487731+00:00 app[web.1]: npm ERR! errno 1
2019-09-06T21:31:54.488648+00:00 app[web.1]: npm ERR! paymentPlatform@0.3.0 build: `babel ./src/index.js -d dist`
2019-09-06T21:31:54.488743+00:00 app[web.1]: npm ERR! Exit status 1
2019-09-06T21:31:54.488862+00:00 app[web.1]: npm ERR!
2019-09-06T21:31:54.488954+00:00 app[web.1]: npm ERR! Failed at the paymentPlatform@0.3.0 build script.
2019-09-06T21:31:54.489097+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-09-06T21:31:54.496430+00:00 app[web.1]: 
2019-09-06T21:31:54.496561+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-09-06T21:31:54.496627+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-09-06T21_31_54_490Z-debug.log
2019-09-06T21:31:54.503294+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-09-06T21:31:54.503506+00:00 app[web.1]: npm ERR! errno 1
2019-09-06T21:31:54.504677+00:00 app[web.1]: npm ERR! paymentPlatform@0.3.0 start: `npm run build && node dist/index.js`
2019-09-06T21:31:54.504826+00:00 app[web.1]: npm ERR! Exit status 1
2019-09-06T21:31:54.505007+00:00 app[web.1]: npm ERR!
2019-09-06T21:31:54.505170+00:00 app[web.1]: npm ERR! Failed at the paymentPlatform@0.3.0 start script.
2019-09-06T21:31:54.505327+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-09-06T21:31:54.511005+00:00 app[web.1]: 
2019-09-06T21:31:54.511183+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-09-06T21:31:54.511305+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-09-06T21_31_54_506Z-debug.log
2019-09-06T21:31:54.562032+00:00 heroku[web.1]: Process exited with status 1

标签: node.jsherokumern

解决方案


推荐阅读