Runtime.ImportModuleError-尝试导入本地util文件时在lambda服务器上获取错误

时间:2019-08-06 18:57:24

标签: node.js aws-lambda aws-serverless

我在handler.js文件中包含了一个utils文件。它在本地运行良好,但是在使用lambda中的无服务器部署它后进行测试时,会引发以下错误-

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'myModules/utils/bCryptPwd'",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'myModules/utils/bCryptPwd'",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:36:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:776:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)",
        "    at Module.load (internal/modules/cjs/loader.js:653:32)",
        "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
        "    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)",
        "    at startup (internal/bootstrap/node.js:283:19)",
        "    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)"
    ]
}

请注意,我正在使用的文件名为“ bCryptPwd”。我将文件夹移动到node_modules,并在如下所示的handler.js函数中使用它。

const {bCryptPwd} = require('myModules/utils/bCryptPwd');

我希望nodejs能够像在本地一样工作,但是没有。任何帮助将不胜感激。

添加package.json。我最近进行了此初始化,因为无服务器未创建package.json

{
  "name": "lamdaserver",
  "version": "1.0.0",
  "description": "",
  "main": "handler.js",
  "directories": {
    "test": "test"
},
  "dependencies": {
    "bcrypt": "^3.0.6",
    "bcryptjs": "^2.4.3",
    "mongoose": "^5.6.8"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

0 个答案:

没有答案