【问题标题】:How to resolve module not found on Heroku?如何解决 Heroku 上找不到的模块?
【发布时间】:2019-04-25 16:13:15
【问题描述】:

我在我的 nodejs 应用程序中使用模块别名。 本地一切正常,正在找到文件路径和模块。

但是,在 Heroku 上部署时,我的别名都没有被识别,并且发生了这个错误

Error: Cannot find module 'app/models/socket.js' app[web.1]: at Function.Module._resolveFilename (module.js:536:15) app[web.1]: at Function.Module._load (module.js:466:25) app[web.1]: at Module.require (module.js:579:17) app[web.1]: at require (internal/module.js:11:18) app[web.1]: at Object.<anonymous> (/app/bin/www:17:21) app[web.1]: at Module._compile (module.js:635:30) app[web.1]: at Object.Module._extensions..js (module.js:646:10) app[web.1]: at Module.load (module.js:554:32) app[web.1]: at tryModuleLoad (module.js:497:12) app[web.1]: at Function.Module._load (module.js:489:3) app[web.1]: npm ERR! code ELIFECYCLE app[web.1]: npm ERR! errno 1

package.json

{
  "name": "nameless",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "commit": "commit-wizard"
  },
  "engines": {
    "node": "8.9.1"
  },
  "dependencies": {
    "apn": "^2.2.0",
    "apn-test": "^1.3.1",
    "app-module-path": "^2.2.0",
    "async": "^0.9.0",
    "await-timeout": "^0.3.0",
    "aws-subdomain": "^0.1.2",
    "base64-img": "^1.0.3",
    "bcrypt": "^0.8.7",
    "bluebird": "^2.10.2",
    "body-parser": "~1.8.1",
    "change-case": "^2.1.6",
    "circle-to-polygon": "^1.0.0",
    "cookie-parser": "~1.3.3",
    "cors": "^2.8.4",
    "cron": "^1.3.0",
    "debug": "^4.1.0",
    "diet": "^0.16.0",
    "ejs": "^2.5.7",
    "express": "~4.9.0",
    "express-async-handler": "^1.1.4",
    "express-fileupload": "^0.3.0",
    "express-fileuploader-s3": "0.0.1",
    "express-sslify": "^1.2.0",
    "express-subdomain": "^1.0.2",
    "fb": "^1.1.1",
    "fcm-node": "^1.2.1",
    "fcm-push": "^1.1.3",
    "geocoder": "^0.2.3",
    "geojson-area": "^0.2.1",
    "geojson-polygon-center": "0.0.0",
    "geojson-rewind": "^0.2.0",
    "google-libphonenumber": "^3.0.12",
    "google-locations": "^0.1.2",
    "googleplaces": "^0.6.0",
    "graceful-fs": "^4.1.15",
    "gulp": "^3.8.10",
    "gulp-concat": "^2.6.0",
    "gulp-ng-annotate": "^1.0.0",
    "gulp-nodemon": "^1.0.5",
    "gulp-replace-task": "^0.2.1",
    "gulp-run": "^1.6.6",
    "gulp-sloc": "^1.0.4",
    "gulp-sourcemaps": "^1.12.1",
    "gulp-stylus": "^2.7.0",
    "gulp-uglify": "^1.2.0",
    "http": "0.0.0",
    "http-request": "^0.7.0",
    "instagram-api": "^1.0.3",
    "instagram-node": "git+https://github.com/andrewoodleyjr/instagram-node.git",
    "instagram-node-api": "^1.6.0",
    "instagram-node-lib": "^0.1.1",
    "jade": "~1.6.0",
    "json2csv": "^2.12.0",
    "jwt-simple": "^0.2.0",
    "knox": "^0.9.2",
    "linkedin-scraper2": "https://github.com/andrewoodleyjr/node-linkedin-scraper2/tarball/master",
    "mobile-detect": "^1.4.1",
    "module-alias": "^2.1.0",
    "mongoose": "^5.0.4",
    "mongoose-query-random": "^1.0.1",
    "mongoose-random": "^0.1.1",
    "mongoose-simple-random": "^0.4.1",
    "morgan": "~1.3.0",
    "multer": "^0.1.6",
    "ng-intl-tel-input": "^2.0.0",
    "node-cron": "^1.2.1",
    "node-foursquare": "^0.3.2",
    "node-foursquare-venues": "^1.1.0",
    "node-instagram": "^2.0.2",
    "node-linkedin": "git+https://github.com/andrewoodleyjr/node-linkedin.git",
    "nodemailer": "^1.3.0",
    "nodemailer-ses-transport": "^1.5.1",
    "nodemon": "^1.14.12",
    "opbeat": "^4.17.0",
    "polygon-centroid": "^1.0.0",
    "prerender-node": "^2.7.4",
    "q": "^1.5.1",
    "quickthumb": "0.0.11",
    "raw-body": "^2.3.2",
    "request": "^2.83.0",
    "sendy-api": "git://github.com/igord/sendy-api.git",
    "serve-favicon": "^2.4.5",
    "socket.io": "1.7.2",
    "stripe": "^6.3.0",
    "swagger": "0.0.1",
    "swagger-node-express": "^2.1.3",
    "swagger-tools": "^0.9.0",
    "tempie": "0.0.2",
    "timezone-compute": "^0.2.2",
    "timezoner": "^0.1.9",
    "twilio": "^3.11.3",
    "validator": "^3.27.0",
    "vhost": "^3.0.2",
    "webpack": "^4.26.0",
    "webpack-node-externals": "^1.7.2"
  },
  "devDependencies": {
    "eslint": "^5.9.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-import-resolver-alias": "^1.1.1",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.1.2",
    "eslint-plugin-react": "^7.11.1",
    "prettyjson": "^1.2.1"
  },
  "_moduleAliases": {
    "@root": "./",
    "@db": "db/",
    "@models": "models/",
    "@util": "util/",
    "db": "db/db.js",
    "config.js": "config.js"
  }
}

bin/www

require('module-alias/register');

const SocketModel = require('@models/socket.js');
const app = require('@root/app.js');

【问题讨论】:

  • 您找到了解决方案?我目前无法将我的应用部署到 hero 并且它也在使用模块别名
  • @The.Wolfgang.Grimmer 很遗憾没有
  • 我添加了一个暂定的解决方法。看看它是否适用于你的情况

标签: javascript node.js heroku relative-path module-alias


【解决方案1】:

您需要像这样删除“_moduleAliases”下路径末尾的斜杠:

"_moduleAliases": {
 "@root": ".",
 "@db": "db",
 "@models": "models",
 "@util": "util",
 "db": "db/db.js",
 "config.js": "config.js"
}

【讨论】:

  • 感谢您的回复。不幸的是它没有解决问题...同样的错误
  • 您是否也将模型文件夹推送到heroku?据我所知,heroku 不安装依赖项以外的任何东西
  • 是的,除了依赖项之外的所有内容都被推送到heroku
  • @andrewoodleyjr 你的文件夹结构是什么?
【解决方案2】:

注意,如果还没有解决方案,这只是最后的手段。

因为 OP 说它在本地构建和工作。

让 typescript 忽略那些特定的导入。为每个带有别名的导入添加 //@ts-ignore。我知道这是一个丑陋的解决方案,但到目前为止没有任何效果。 请注意,此解决方法仍需要 module-alias

既然你说它在本地工作,那么我相信让 typescript 在构建期间忽略这些文件并不是什么大不了的事,因为我们关心的只是文件被编译为 javascript。当然,你需要对编译好的js文件进行测试才能确定。

此问题与 typescript 在构建期间未将路径映射解析为其相应路径有关。

您可以找到更多信息here。如果您想手动转换已编译文件上的这些路径映射,请检查该答案上的 cmets。

另一个相关的issue

【讨论】:

  • 工作。谢谢!
  • PS:但我没有使用module-alias
猜你喜欢
  • 2020-04-29
  • 2021-10-13
  • 2019-09-10
  • 2022-09-23
  • 2015-06-21
  • 1970-01-01
  • 2020-02-23
  • 2021-10-05
  • 2021-08-31
相关资源
最近更新 更多