AWS Lambda 导出类在 node.js v6.4 中有效,但在 node.js v4.3 中无效,如何解决这个问题? [复制]
Posted
技术标签:
【中文标题】AWS Lambda 导出类在 node.js v6.4 中有效,但在 node.js v4.3 中无效,如何解决这个问题? [复制]【英文标题】:AWS Lambda exports class works in node.js v6.4 but not in node.js v4.3, how to fix this? [duplicate] 【发布时间】:2017-03-12 21:06:49 【问题描述】:我在 node.js v6.4 中有代码工作: 只有两个文件,index.js:
// ------------ Index.js ------------
'use strict';
var Event = require('./models/event.js');
exports.handler = (event, context, callback) =>
console.log('done');
和 event.js:
// ------------ Event.js ------------
class Event
static get dynamoDBTableName()
return
get hashValue()
return
parseReference(reference)
return
exports.Event = Event
在使用 node.js 4.3 版本的 AWS Lambda 上运行 index.handler
时,会引发错误:
Syntax error in module 'index': SyntaxError
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/index.js:16:13)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
我认为exports.Event = Event
有问题,
有什么技巧可以解决这个问题。
我是 node.js 的新手。
任何帮助都应该感激。
我认为这不是 (event, context, callback) =>
的 SyntaxError
因为 AWS Lambda 示例代码使用此语法运行良好:
【问题讨论】:
OP 更新了原始问题,并且(正确地)补充说箭头函数不是罪魁祸首。我的错误,我在下面给出了错误的答案,从而误导了社区将该问题标记为重复。我认为应该重新提出这个问题。 【参考方案1】:我原本以为箭头函数是罪魁祸首。但是,AWS Node.js 4.3.2 确实支持箭头功能,如 post about Node.js 4.3.2 Runtime on Lambda 中所述。
新(正确)答案
event.js 文件是否以'use strict';
开头?
您必须在 node.js 4.3.2 中对类声明使用严格模式
Mozilla Developer Network about strict mode
希望这会有所帮助...
原始(不正确)答案
module.exports = 产品
我相信箭头函数:
() =>
尚未在您使用的nodejs版本(4.3)中实现。
See this answer
Node.js 从 4.4.5 版本开始支持箭头函数
如果更新你的 nodejs 版本不是你的选择,你可以替换:
exports.handler = (event, context, callback) =>
console.log('done');
与
exports.handler = (event, context, callback) = function()
console.log('done');
【讨论】:
感谢您的快速响应,但很抱歉这可能无济于事。更改为function(event, context, callback) ...
后,再次抛出相同的错误。
@beeth0ven 你是对的,AWS Nodejs 4.3 中似乎有箭头功能:aws.amazon.com/blogs/compute/… 因此我的回答是错误的,你的问题可能不是重复的;我会删除我的答案,但是这些 cmets 会消失,不是吗?
在节点 v5.1.1 上测试代码后,抛出错误 SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
,所以我将 'use strict'
添加到文件 event.js。然后就可以了。谢谢你给我重要的信息。您能否将答案更新为:“只需将 use strict 添加到文件 event.js`。我会将您的答案标记为正确。谢谢。
很高兴问题得到解决:)以上是关于AWS Lambda 导出类在 node.js v6.4 中有效,但在 node.js v4.3 中无效,如何解决这个问题? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
使用 Node.JS 调用 AWS 胶水的 lambda 函数不使用 console.log 的原因是啥?
有没有办法从 node.js 同步调用 AWS Lambda?
如何检查 AWS Lambda 和 Elasticbeanstalk 中的次要 Node.js 版本?
如何从 AWS lambda 发布到 Node.js 中的云观察指标