我已经使用无服务器在 NodeJS 中创建了一个 lambda 函数处理程序。当我使用命令:serverless offline start 时,我没有收到任何错误。但是,当我想使用无服务器部署命令部署应用程序时,它部署得很好。当我想去端点时,我收到内部服务器错误,只有在我的应用程序中需要 camaro 时才会发生这种情况。我需要 camaro 库来从 XML 创建模板。

我尝试使用 Node 6.10 并删除 camaro,并使用 Node 6.10 安装它。这没什么区别。

这是我可以在 Cloud watch 中查看的错误:



这是我的 index.js

const serverless = require('serverless-http');

///
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();



// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
//serverless
module.exports.handler = serverless(app);

这是我的 serverless.yml
# serverless.yml
service: lambda-dashboardcb

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: eu-west-1

functions:
  app:
    handler: index.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
plugins:
  -  serverless-offline

这是我的 package.json
package.json
{
  "name": "lambda-dashboardcb",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon ./bin/www"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.17.1",
    "body-parser": "^1.18.2",
    "camaro": "^2.2.2",
    "cookie-parser": "^1.4.3",
    "debug": "~2.6.9",
    "express": "^4.15.5",
    "google-oauth-jwt": "^0.2.0",
    "googleapis": "^23.0.0",
    "morgan": "^1.9.0",
    "pug": "^2.0.0-beta11",
    "serve-favicon": "^2.4.5",
    "serverless-http": "^1.5.2"
  },
  "devDependencies": {
    "nodemon": "^1.13.3",
    "serverless-offline": "^3.16.0"
  }
}

路由器
var express = require('express');
var router = express.Router();
var metadata = require('../public/javascripts/metadata-onix.js');
var path = require("path");

var app = express();

/* GET home page. */
router.get('/', function(req, res, next) {
    res.sendFile(path.join(__dirname, '../public', 'index.html'));
});

router.param('isbn', function (req,res,next){
    next();
});

router.get('/metadata:isbn', function(req,res,next){
    /**
        - Get ISBN From URL
        - Get Metadata from ISBN, return as JSON
    **/
    var isbn = req.params.isbn;

    var info = metadata.getMetadataOnix(isbn).then(function(info) {
        res.json({ cover : info });
    });
});

module.exports = router;

使用 Camaro 的模块
const axios = require('axios');
const transform = require('camaro');

exports.getMetadataOnix = function (id) {
    /**
        - Create template
        - Get request to content CB API.
        - Transform data with template.
        - Return data.
    **/
    const template = {
       template stuff...
    }

    return axios({
        method: 'get',
        url: 'APIURL'+id,
        transformResponse: [function (data) {
            "use strict";
            data = transform(data, template);
            return data;
        }],
        // No .catch here
    }).then(function(resp){
        console.log('Cover from book:'+JSON.stringify(resp.data));
        return resp.data;
    });
}

最佳答案

camaro 是一个原生模块。您需要安装正确的 prebuilt binary for AWS Lambda
即使您在本地切换到 Node 6.10,但安装在您机器上的二进制文件仅为您的平台构建,这可能与 AWS Lambda 上的平台不同。

Lambda 现在已经支持 Node 8,因此请相应地选择预构建的二进制文件。

关于node.js - AWS Lambda : module initialization error: Error when running Camaro/Serverless,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47891144/

10-16 18:34