[英]Append a directory to Amazon Lambda Environment Variables $NODE_PATH
Default Env Variable as per the docs :根据文档的默认环境变量:
NODE_PATH:/opt/nodejs/node8/node_modules/:/opt/nodejs/node_modules:$LAMBDA_RUNTIME_DIR/node_modules
I want to append my custom directory to it (NOT to override all)我想将我的自定义目录附加到它(不是覆盖所有)
NODE_PATH:$NODE_PATH:/opt/nodejs/mycustom-directory
I tried the above from lambda console it overrides all.我从 lambda 控制台尝试了上面的内容,它覆盖了所有内容。
$NODE_PATH
is added as a string. $NODE_PATH
作为字符串添加。 It is not parsing $NODE_PATH
它没有解析
$NODE_PATH
Output I got when printing env:打印 env 时得到的输出:
NODE_PATH=$NODE_PATH:/opt/nodejs/mycustom-directory
Similar Question but no Solution still: AWS lambda add PATH variable?类似的问题,但仍然没有解决方案: AWS lambda 添加 PATH 变量?
I came up with the same problem.我想出了同样的问题。 I wrote a code to check if the lambda function applies the shell variable expansions, and to figure out they don't.
我写了一段代码来检查 lambda 函数是否应用了 shell 变量扩展,并找出它们没有。
I set environment variable FOO
as $NODE_PATH
,我将环境变量
FOO
设置为$NODE_PATH
,
Then, run the check code (in lambda function):然后,运行检查代码(在 lambda 函数中):
const { FOO } = process.env;
exports.lambdaHandler = async (event, context, callback) => {
console.log(FOO);
};
The output is:输出是:
2019-02-22T08:29:05.714Z cde21239-628f-4a79-b046-6a14f177f59e $NODE_PATH
I just rewrite the whole NODE_PATH
to be (my custom library path):/opt/nodejs/lib:/opt/nodejs/node8/node_modules/:/opt/nodejs/node_modules:/var/runtime/node_modules
我只是将整个
NODE_PATH
重写为(my custom library path):/opt/nodejs/lib:/opt/nodejs/node8/node_modules/:/opt/nodejs/node_modules:/var/runtime/node_modules
the default value of NODE_PATH
is explained there: NODE_PATH
的默认值在NODE_PATH
解释:
https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
I needed this too, as I am using AWS Lambda Layers, which are placed on the default NODE_PATH
, and wanted to also be able to use local roots to avoid long relative paths (such as import bar from foo/bar
instead of import bar from ../../../../foo/bar
, but I didn't find any way to append to NODE_PATH
without losing the default ones - as soon as it's set, the paths to node_modules
- including the aws-sdk
module, are lost.我也需要这个,因为我使用 AWS Lambda 层,它被放置在默认的
NODE_PATH
,并且希望还能够使用本地根来避免长相对路径(例如import bar from foo/bar
而不是import bar from ../../../../foo/bar
,但我没有找到任何方法来附加到NODE_PATH
而不丢失默认路径 - 一旦设置, node_modules
的路径 - 包括aws-sdk
模块,丢失。
The only solutions I can think of are:我能想到的唯一解决方案是:
Explicitly set NODE_PATH
to the default value plus your custom one (which adds an ugly dependency to lambda internal environment configuration which you shouldn't have to care about)将
NODE_PATH
显式设置为默认值加上您的自定义值(这为您不必关心的 lambda 内部环境配置添加了一个丑陋的依赖项)
Put your custom library in a layer.将您的自定义库放在一个图层中。 Many times this is a good solution if you can extract sub-modules as separate layers (but it doesn't help for situations like elimination of long relative paths within the application itself like I described above).
很多时候,如果您可以将子模块提取为单独的层,这是一个很好的解决方案(但它对消除应用程序本身内的长相对路径等情况没有帮助,就像我上面描述的那样)。
Append programatically, by having the very first lines of your application do以编程方式追加,通过让您的应用程序的第一行做
process.env.NODE_PATH = process.env.NODE_PATH + ":my-custom-path";
require("module").Module._initPaths(); // This re-initalizes the module loader to use the new NODE_PATH.
require('some-custom-module-in-my-custom-path'); // should work
require('aws-sdk') // should also work
This may not be the prettiest hack, but it should do the trick (disclaimer: haven't actually tried this in a AWS Lambda environment but it works locally using Node 12 at least).这可能不是最漂亮的 hack,但它应该可以解决问题(免责声明:实际上并没有在 AWS Lambda 环境中尝试过,但它至少使用 Node 12 在本地工作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.