0

I am new to nodeJS, npm, and aws lambda. Suppose I have a custom sort algorithm that I want to use in several lambda functions. This should be something very simple, but the only way I have found is to create a layer with a node module published as an npm package. I do not want any of the code be uploaded to an npm.

I tried to download the layer I am currently using, create a folder in node_modules along with other packages that are published in npm with

  1. npm init
  2. fill all the info for the package.json
  3. created a function code in a index.js
    'use strict'    
    exports.myfunc= myfunc;  
    function myfunc() {
        console.log("This is a message from the demo package");
    }
  1. zip all the layer again and upload it in a version 2 of the layer

  2. pick the new version in a lambda function and call it as i would do with any other node_module form a third party, like this:

    const mypack= require('mypack'); mypack.myfunc();

but it tells me:

"errorMessage": "Error: Cannot find module ... \nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",

I think it maybe because in the layer in the nodejs folder, there is package-lock.json and my module is not there. I tried to put it there without the "resolved" and "integrity" that the packages published on npm have but did not work.

Is there a way to simply upload the code I want in a nodejs layer having nothing to do with npm?

I would like to try something such as this in the accepted answer, var moduleName = require("path/to/example.js") How to install a node.js module without using npm?

but I don't know where is the path of the modules of a layer, the path of lambda that shows __dirname is /var/task, but it looks like any lambda has same path. I am lost...

1
  • If you're using a monorepo solution, you could also forgo working with layers and use for example lerna to manage your packages locally. Commented Aug 25, 2021 at 7:10

1 Answer 1

0

I was able to import js in a folder in the following manner:

download the zip of the layer uncompress it, put in node_modules any additional folder with your custom js and upload it as a new version of the layer. In the lambda function, reference it with moduleName = require("path/to/example.js")

You can find that path, which is the trick here, using a known library you already have in your layer and is working, in my case I used base64-js, then I returned the path of that library like this:

require.resolve('base64-js')

That returned

'/opt/nodejs/node_modules/base64-js/index.js'

So I used

moduleName = require("/opt/nodejs/node_modules/MYCUSTOMFOLDER/index.js")

and that was it...

Sign up to request clarification or add additional context in comments.

Comments

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.