最近我发现在AWS Lambda nodejs8.10中预装了aws-sdk
NPM模块。我在互联网上找不到任何关于它的信息。
AWS Lambda中预安装了哪些其他node.js模块?
我找不到正式列表,所以我编写了一个脚本来创建列表。目前这些是(不包括内置的nodejs模块,当然也可以使用):
'awslambda',
'aws-sdk',
'base64-js',
'dynamodb-doc',
'ieee754',
'imagemagick',
'isarray',
'jmespath',
'lodash',
'sax',
'uuid',
'xml2js',
'xmlbuilder'
生成此列表的代码:
function flatten(arrayOfArrays) {
return Array.prototype.concat.apply([], arrayOfArrays)
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function extPackageNames(node) {
if (!node.children) return [];
const arrayOfArrays = node.children.map(c => [c.name].concat(extPackageNames(c)))
const result = flatten(arrayOfArrays)
return result
}
exports.handler = async (event) => {
const rpt = require("read-package-tree")
const module = require("module")
const pathArg = process.env.NODE_PATH
const allPaths = pathArg.split(":")
// '/var/task' is this package on lambda
const externalPaths = allPaths.filter(p => p !== "/var/task")
// read all package data
const ps = externalPaths.map((path) => rpt(path).catch(err => err))
const rpts = await Promise.all(ps).catch(err => err)
// extract the information we need
const packagesPerPath = rpts.map(extPackageNames)
const countPerPath = packagesPerPath.map(arr => arr.length)
const packages = flatten(packagesPerPath)
// remove duplicates
const uniquePackages = packages.filter(onlyUnique)
// remove node.js built-in modules
const uniqueCustomPackages = uniquePackages.filter(p => !module.builtinModules.includes(p))
const result = {
node_path: pathArg,
paths: externalPaths.map((e, i) => [e, countPerPath[i]]),
uniqueCustomPackages
}
console.log(result)
const response = {
statusCode: 200,
body: JSON.stringify(result)
};
return response;
};
要在lambda上运行它,您需要将其与包含node_modules
的read-package-tree
文件夹一起压缩。