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. $NODE_PATH
is added as a string. It is not parsing $NODE_PATH
Output I got when printing env:
NODE_PATH=$NODE_PATH:/opt/nodejs/mycustom-directory
Similar Question but no Solution still: AWS lambda add PATH variable?
Lambda functions come with a set of default environment variables. These environment variable names are reserved, and users will not be able to overwrite them.
According to the official AWS Lambda documentation, environment variables are a tool that allows you to adjust your function's behavior without updating the lambda's code. “An environment variable is a pair of strings that is stored in a function's version-specific configuration.
To change the runtime, you create a new container image. When you use a . zip file archive for the deployment package, you choose a runtime when you create the function. To change the runtime, you can update your function's configuration.
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.
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)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With