我如何告诉lambda所有npm依赖项?

时间:2017-12-16 23:19:25

标签: node.js aws-lambda

我正在阅读本教程,了解如何编写节点restful app

https://github.com/mjhea0/node-postgres-promises

我想将此应用程序作为lambda函数运行。我担心的是依赖管理。

如何告诉lambda所有npm依赖项?

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 pug = require('pug');
var routes = require('./routes/index');

在教程中他们做npm install并且aws没有用,我们没有像这样的cli命令的概念。

3 个答案:

答案 0 :(得分:3)

您可以在本地计算机上开发应用程序,并在您构建的项目中安装所有npm依赖项(在本地计算机中)。完成开发后,压缩所有文件(包括package.config和npm_modules)并将其上传到lambda。压缩文件时请注意

请不要压缩根文件夹。打开根文件夹,选择里面的所有文件和文件夹,然后右键单击zip all。

答案 1 :(得分:1)

您在本地计算机上执行 int daemon(int nochdir, int noclose); (理想情况下是Linux)。然后,在创建部署包时,您将在其中包含npm install文件夹。

上传该部署包(zip文件)后,Lambda将会知道您的node_modules并将其与您的处理程序一起提取。

答案 2 :(得分:1)

In addition to other users who basically gave you the correct answer, I’d suggest trying out Claudia.js, which is a deployment tool for AWS Lambda (at least when used with Node.js).

Using Claudia, you neither have to set up things in the AWS UI, nor do have to worry about packaging stuff. The only downside is that the package that Claudia creates is larger than a hand-built package of the same code, but I’d say this is a neglectable issue given its advantages.

See https://claudiajs.com/ for more info.