【问题标题】:running node app with exports.object from command line and lambda从命令行和 lambda 使用 export.object 运行节点应用程序
【发布时间】:2015-08-08 06:45:34
【问题描述】:

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

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

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

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

文件index.js

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

【问题讨论】:

    标签: node.js aws-lambda


    【解决方案1】:

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

    解决方法是这样的:

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

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

    $ node index.js somethinghere
    [ '/path/to/node',
      '/path/to/index.js',
      'somethinghere' ]
    
    $ node
    > require('./index')
    { imageRs: [Function] }
    

    【讨论】:

    • 当我这样做时,Lambda 仍然会抱怨。我得到了它与if (!process.env.LAMBDA_TASK_ROOT) { exports.imageRs(); }
    • @hyprstack 谢谢。它对我有用。将其添加为已接受的答案
    猜你喜欢
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多