You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Elastic Beanstalk上的ERR_MODULE_NOT_FOUND错误?

问题:Win10开发ES6风格Express API部署到Linux环境Elastic Beanstalk出现ERR_MODULE_NOT_FOUND错误

我在Win10系统的IDE中开发ES6风格的Express API,本地测试一切正常,但部署到Linux系统的Elastic Beanstalk(EBS)进行生产环境测试时,出现了ERR_MODULE_NOT_FOUND错误,涉及cors、bcrypt、randomstring等部分npm库,但并非所有库都有问题。请问是否因为EBS使用Linux系统导致ES6程序运行存在差异?该如何解决?

完整错误信息

Sep  6 10:14:49 ip-172-31-26-145 web: > node www.js
Sep  6 10:14:49 ip-172-31-26-145 web: internal/process/esm_loader.js:74
Sep  6 10:14:49 ip-172-31-26-145 web: internalBinding('errors').triggerUncaughtException(
Sep  6 10:14:49 ip-172-31-26-145 web: ^
Sep  6 10:14:49 ip-172-31-26-145 web: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'cors' imported from /var/app/current/www.js
Sep  6 10:14:49 ip-172-31-26-145 web: at new NodeError (internal/errors.js:322:7)
Sep  6 10:14:49 ip-172-31-26-145 web: at packageResolve (internal/modules/esm/resolve.js:732:9)
Sep  6 10:14:49 ip-172-31-26-145 web: at moduleResolve (internal/modules/esm/resolve.js:773:18)
Sep  6 10:14:49 ip-172-31-26-145 web: at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:887:11)
Sep  6 10:14:49 ip-172-31-26-145 web: at Loader.resolve (internal/modules/esm/loader.js:89:40)
Sep  6 10:14:49 ip-172-31-26-145 web: at Loader.getModuleJob (internal/modules/esm/loader.js:242:28)
Sep  6 10:14:49 ip-172-31-26-145 web: at ModuleWrap. (internal/modules/esm/module_job.js:76:40)
Sep  6 10:14:49 ip-172-31-26-145 web: at link (internal/modules/esm/module_job.js:75:36) {
Sep  6 10:14:49 ip-172-31-26-145 web: code: 'ERR_MODULE_NOT_FOUND'
Sep  6 10:14:49 ip-172-31-26-145 web: }
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR! code ELIFECYCLE
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR! errno 1
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR! timesheet-api@0.0.0 start: `node www.js`
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR! Exit status 1
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR!
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR! Failed at the timesheet-api@0.0.0 start script.
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR! A complete log of this run can be found in:
Sep  6 10:14:49 ip-172-31-26-145 web: npm ERR!     /home/webapp/.npm/_logs/2022-09-06T10_14_49_923Z-debug.log

库导入方式

import express from "express";
import path from "path";
import cookieParser from "cookie-parser";
import logger from "morgan";
import { fileURLToPath } from "url";
import passport from "passport";
import passportConfig from "./modules/passport.js";
import Debug from "debug";
import Cors from "cors";

package.json内容

{
  "name": "test-api",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  },
  "dependencies": {
    "bcrypt": "^5.0.1",
    "big.js": "^6.2.1",
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "debug": "^2.6.9",
    "dotenv": "^16.0.1",
    "express": "^4.17.1",
    "https": "^1.0.0",
    "jsonwebtoken": "^8.5.1",
    "knex": "^2.0.0",
    "lodash": "^4.17.21",
    "module-alias": "^2.2.2",
    "moment": "^2.29.3",
    "moment-timezone": "^0.5.34",
    "morgan": "~1.9.1",
    "nodemon": "^2.0.16",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pdfmake": "^0.2.5",
    "randomstring": "^1.2.2",
    "tedious": "^14.5.0",
    "uuid": "^8.3.2"
  }
}
解决方案
  • 启用ES6模块支持:在package.json中添加"type": "module"字段。Node.js默认采用CommonJS模块规范,不加该字段的话,.js文件中的import语法会被解析异常,导致模块查找失败。修改后的package.json应包含:

    {
      "type": "module",
      // 其他原有字段...
    }
    
  • 统一启动脚本配置:错误日志显示EBS实际运行的是node www.js,但package.json的start命令是node app.js,需确保两者一致。要么修改package.json的start命令为node www.js,要么调整EBS的启动配置使用package.json中定义的start脚本。

  • 禁止上传node_modules目录:Windows环境下安装的依赖(如bcrypt这类含二进制文件的模块)与Linux环境不兼容。部署时不要将本地的node_modules文件夹打包上传,让EBS在Linux环境下执行npm install重新安装适配的依赖。可通过.gitignore文件添加node_modules/来忽略该目录。

  • 同步Node.js版本:本地与EBS的Node.js版本尽量保持一致,不同版本对ES模块的支持可能存在差异。可在package.json中添加engines字段指定版本,例如:

    "engines": {
      "node": ">=16.x"
    }
    

    EBS会自动使用符合要求的Node.js版本。

  • 检查本地模块路径大小写:Linux系统是大小写敏感的,而Windows不是。确保本地模块的导入路径(如./modules/passport.js)的文件名大小写与部署后的文件完全一致,避免因大小写不匹配导致模块找不到。


内容的提问来源于stack exchange,提问作者Suspended

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:30:51