0

我还在学习中node,遇到过这个问题。在下面的情况下,并使用一个愚蠢的例子(完整的代码不能放在这里),当我在终端中运行时node index.js somethinghere,代码没有执行。我意识到这一点,event并且context在这个例子中没有任何影响,但它们在我目前正在编写的代码中确实如此。

这是因为我在做什么exports.imageRs吗?

我如何通过传入参数让它在命令行上运行?

请注意,原始代码将同时aws lambda在命令行上运行。

文件index.js

exports.imageRs = function (event, context) {
  console.log(process.argv);
}
4

1 回答 1

1

在您展示的示例中,Node 将定义exports.imageRs函数,但不会执行它。

修复是这样的:

exports.imageRs = function (event, context) {
  console.log(process.argv);
};

if (!module.parent) {
  exports.imageRs();
}

!module.parentcheck 防止内部代码在其他模块需要您的模块时执行,这可能是您想要的。

$ node index.js somethinghere
[ '/path/to/node',
  '/path/to/index.js',
  'somethinghere' ]

$ node
> require('./index')
{ imageRs: [Function] }
于 2015-05-26T15:36:21.093 回答