5

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

我有一个基于条件的 API 将发布到另一个 API,但我在包装 API 中收到此错误。

这是代码...

handler.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)
4

4 回答 4

8

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

于 2019-03-12T20:44:04.140 回答
3

好的,我通过删除我的 package.json 然后再次添加它并安装 NOT作为我的包的开发依赖项来解决它并且它有效。

于 2018-09-03T09:35:30.627 回答
2

就我而言,事实证明我将 python 和 nodejs 用于我的 lambda,但没有将 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
于 2019-09-12T08:18:47.090 回答
2

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

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

请仔细检查您所有的模块导入路径,看看它们是否准确。

于 2019-05-15T11:08:55.077 回答