无法在Node.js中导入模块``处理程序''AWS Lambda函数

时间:2018-09-03 09:03:11

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

我不断收到此错误,但我不知道是什么原因造成的。

我有一个API,该API可根据条件发布到另一个API, 但我在包装API中遇到此错误。

这是代码...

handler.js

ipcRenderer.on('asynchronous-message', function (event, message) {
    if (message == 'PREFERENCE_SAVED') {
        document.querySelector('html').style.setProperty("--background", "orange");
    }
});

serverless.yaml

'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 {
    //...
  };
};

任何建议将不胜感激。

错误消息:

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

4 个答案:

答案 0 :(得分:3)

好吧,我通过删除package.json,然后再次添加并安装 NOT 作为我的软件包的开发依赖性来解决了该问题,

答案 1 :(得分:1)

该错误消息并没有太大帮助,但是我发现该消息通常是由丢失的npm软件包引起的。如果您在AWS控制台中测试lambda,则可以查看特定详细信息。

答案 2 :(得分:1)

在我的情况下,事实证明,我在lambda中同时使用了python和nodejs,但是没有将nodejs lambda函数的运行时环境设置为nodejs。以前,我的 serverless.yml 看起来与此类似:

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

只需为nodejs lambda提供运行时环境即可解决此问题:

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

答案 3 :(得分:0)

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

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

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