1

This is a question about how to manage a project with functions in separate files.

main file

const AWS = require('aws-sdk');
const monkeyDynamodb = require("../includes/dynamodb/monkey-dynamodb-functions.js")
...
exports.handler = async function(event, context) {
    let billingDevices = monkeyDynamodb.scan("SensorNodeBilling");
    ....

monkey-dynamodb-functions.js

async function scan(dbName) {
    var docClient = new AWS.DynamoDB.DocumentClient();
    

...

If you compile this, you have 2 separate objects called AWS & AWS2, and a warning since one of them is not declared. And if you run it, it fails.

On the other hand, if you add in const AWS = require('aws-sdk'); into monkey-dynamodb-functions.js, it now works, but the compiled code still creates multiple AWS objects. What is the right way to code this?

1 Answer 1

2

You should have this:

const AWS = require('aws-sdk');

in any module that wants to use this AWS SDK. The module system uses a cache so the module itself will only be loaded once (the first time it is required) and from then after will just return the same exports object that was originally loaded.

The rest of your question is not entirely clear as there's no AWS2 object in any of your code that you show so it's unclear what you think you have two of. If you're just talking about the monkey-dynamodb-functions.js file, then yes, it must also require in the AWS module so it can use it.

Unlike in the browser, each of your nodejs files runs in its own module. So, when you do something like this:

const AWS = require('aws-sdk');

in the main file, that AWS symbol is local to only this module. It cannot be accessed directly by any other module. So, in any other module that you also want to use the AWS sdk, just require it into that module also.

You can share objects or functions from one module to others by exporting them from one module and then requiring them into another module from that module and every module that wishes to have access to that shared object would require it in separately. That's what the aws-sdk module is itself doing.

but the compiled code still creates multiple AWS objects

It's not entirely clear what you mean by this. If you mean that when do require in the aws-sdk in both modules, you will then have two separate variables for the AWS sdk, that is true. That is the correct way to program it. Each module contains its own set of local variables. Those two variables will each contain the exact same aws-sdk object reference. This is the correct way to code with modules in nodejs.

Main file

const AWS = require('aws-sdk');
const monkeyDynamodb = require("../includes/dynamodb/monkey-dynamodb-functions.js")
...
exports.handler = async function(event, context) {
    let billingDevices = monkeyDynamodb.scan("SensorNodeBilling");
    ....

monkey-dynamodb-functions.js

const AWS = require('aws-sdk');

async function scan(dbName) {
    var docClient = new AWS.DynamoDB.DocumentClient();
    ...
}

exports.scan = scan;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you've understood me perfectly. I get 2 separate variables for the AWS sdk which looked wrong, so thank you for clarifying that this is expected and not a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.