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

squ*_*ave 4 node.js aws-lambda serverless-framework aws-serverless

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

我有一个根据条件发布到另一个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 {
    //...
  };
};
Run Code Online (Sandbox Code Playgroud)

serverless.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
Run Code Online (Sandbox Code Playgroud)

任何意见,将不胜感激。

错误信息:

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)
Run Code Online (Sandbox Code Playgroud)

aar*_*rio 8

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

  • “如果在AWS控制台中测试lambda,则可以看到特定的详细信息。”此注释为金色。在其他任何地方都找不到此帮助。谢谢@aarkerio (3认同)

squ*_*ave 3

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

  • 从 `package.json` 中删除_什么_?再次添加_什么_? (3认同)