How to use a custom made library

Viewed 25

I have a AWS dependency layer on on folder nodejs/

There is node_modules/ and package.json with npm dependencies

I created a folder called utils/ and my file is util.js

Since it's a layer on AWS, I import using const utils = require('/opt/nodejs/utils/util'); on my app.js

Problem is that my test cases started failing Cannot find module '/opt/nodejs/utils/util' from 'backend/lambdas/cars/app.js'

How can I fix my test case??

const app = require('./app');

describe('lambda', function () {
    it('something', async () => {
        const response = await app.lambdaHandler();
        ....
    });
});

app.js

const httpStatusCode = require('http-status-codes');
const cors = require('/opt/nodejs/utils/util');

exports.lambdaHandler = async (event) => {
return {
    statusCode: httpStatusCode.OK
 };
};

PS: This nodejs folder is on the same level as the lambdas folder

1 Answers

You should import like this const utils = require('../nodejs/utils/util')

Related