代码之家  ›  专栏  ›  技术社区  ›  squeekyDave

无法在nodejs中导入模块“handler”aws lambda函数

  •  3
  • squeekyDave  · 技术社区  · 8 年前

    我不断地得到这个错误,我不知道是什么原因造成的。

    我有一个API,基于一个条件将发布到另一个API,

    处理程序.js

    'use strict';
    const axios = require('axios');
    
    module.exports.thumbnailWrapperAPI = (event, context, callback) => {
    
    
      const incomingData = JSON.parse(event.body);
      if(incomingData.source.includes('png') || incomingData.source.includes('jpg')){
        const newLocal = 'some endpoint...';
        // call image resizing API...
        axios.post(newLocal,{
          source: incomingData.source,
          target: incomingData.target,
          width: incomingData.width
        })
        .then(response => callback(null,response))
        .catch(error => callback(error))
    
      } else if(incomingData.source.includes('html')) {
        // handle HTML
      } else {
        //...
      };
    };
    

    无服务器.yaml

    service: thumbnailWrapperAPI 
    provider:
      name: aws
      runtime: nodejs8.10
      region: eu-west-1
    
    functions:
      thumbnailWrapperAPI:
        handler: handler.thumbnailWrapperAPI
        events:
          - http:
              path: generatethumbnail/
              method: post
              cors: true
    

    如有任何建议,将不胜感激。

    错误消息:

    Unable to import module 'handler': Error
        at Function.Module._resolveFilename (module.js:547:15)
        at Function.Module._load (module.js:474:25)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (/var/task/handler.js:2:15)
        at Module._compile (module.js:652:30)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)
    
    2 回复  |  直到 8 年前
        1
  •  8
  •   aarkerio    7 年前

    这个错误消息没有太大帮助,但是我发现这个消息通常是由丢失的npm包引起的。如果在AWS控制台中测试lambda,可以看到具体的细节。

        2
  •  3
  •   squeekyDave    8 年前

    好吧,我解决了这个问题包.json然后再次添加并安装 不是

        3
  •  2
  •   The Oracle Albert    7 年前

    当需要使用错误路径的模块或文件时,也会出现此错误。换句话说,需要一个不存在的模块/文件。

    它可以是自定义模块或npm。

    请仔细检查所有模块导入路径,并确保它们是正确的。

        4
  •  1
  •   Andreas Forslöw    7 年前

    在我的例子中,结果是我对lambda使用了python和nodejs,但是没有将nodejs lambda函数的运行时环境设置为nodejs。以前,我的 看起来像这样:

    provider:
      name: aws
      runtime: python3.7
      stage: dev
      region: eu-west-1 
      profile: my-profile
    
    functions:
      nodejs-func:
        handler: nodejs_func.handler
        events:
          - websocket:
              route: nodejs-func
      python-func:
        handler: python_func.handler
        events:
          - websocket:
              route: python-func
    

    provider:
      name: aws
      runtime: python3.7
      stage: dev
      region: eu-west-1 
      profile: my-profile
    
    functions:
      nodejs-func:
        handler: nodejs_func.handler
        runtime: nodejs10.x # Provide the runtime environment for lambda func
        events:
          - websocket:
              route: nodejs-func
      python-func:
        handler: python_func.handler
        events:
          - websocket:
              route: python-func