Serverless deployment to AWS Lambda missing modules? - node.js

First time dealing with serverless here. Have successfully deployed using serverless deploy after following the guide to migrating an existing express app over to serverless. But aws lambda keeps throwing an error:
“errorType”: “Runtime.ImportModuleError”,
“errorMessage”: “Error: Cannot find module ‘serverless-http’\nRequire stack:\n- /var/task/app.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js”,
So I’m confused. What am I doing wrong? The guide to converting an existing express app didn’t say we’d need to create an AWS Lambda Layer, but since it seems like Lambda can’t find the serverless-http module, does it mean that creating a layer is the fix?
Edit: my .zip file seems to only be an express.js file. I'm not sure if that's unusual but reading online reveals that most people seem to have a node_modules folder zipped up as well? I presume the lack of a node_module folder in the .zip file is causing this runtime.ImportModuleError fail? How do I get serverless to add a node_modules folder if that's the case?

node_modules folder is generally packaged with your code -- provided it exists in your directory (does it?).
If node_modules isn't installed on your local machine, then you can create it by using the npm install command. This command will install all the dependencies listed in the package-lock.json file (or package.json) -- sorry I'm not a node guy :(.
But it definitely sounds like you're not uploading your node_modules folder because it's not on your local machine. You have to initialize the directory first.

Related

DiscordJS & Heroku: Error: Cannot find module '../Modal'

So this is lately one of the most popular issues in discord.js community. Appears after updating to d.js v13.7.0 and higher. While it works perfectly on a PC or etc., it fails to run on Heroku hosting with the following message:
Error: Cannot find module '../Modal'
This is clear that the issue is on Heroku's side, because Modal file can be found in discord.js dependency folder in node_modules.
I have NodeJS v18.3.0, have tried reinstalling discord.js with multiple methods, nothing helped.
I wasn't able to find any solutions to the problem, have anyone over here found the solution?
Im pretty sure that you didn't create a file called .gitignore because it shows that your problem is on the folders '../Modal', Before deploying it to heroku, you should ignore the 2 files on your project.
node_modules
.env (if you have)
To remove these 2 on your deploying, create a file on your main folder named .gitignore and right down the following:
node_modules
.env

Deploying next app to Azure fails because node_modules need to be re-installed

I'm creating a build in Azure pipelines and copying node_modules as well as 'out' folder, package.json, package-lock.json, next.config.js and .next folder. I copy all of these into an artifact directory.
I then zip this as an artifact and then do a zip deploy to Azure App Service Linux Web app.
However running npm start (which just points to next start in the package.json) doesn't work as it complains about next not being found in node_modules (even though its there)
What do I need to do to get it work?
If I download the zip file and unzip it locally, it doesn't work either even though all the node_modules are there. I need to run npm ci to be able to get next start to work. How come?
Aren't I meant to just be able to copy node_modules across and everything should just work?
Also bonus points - do I need to force next to start on port 8080 on azure? The default port 3000 doesn't seem to work?
And Azure docs say that npm install is run when package.json is detected. However that's clearly not happening either.
Any help and insights would be most appreciated!
If anyone is trying to deploy Next.js SSR apps to Azure Web apps, the issue is the copying of the node_modules and the config of next.config.js
I was using the File Copy task in my yml file. This isn't good enough.
You have to use cp -paR node_modules to your artifact folder.
We don't have a server.js file but you can't use serverless unless you create a server.js file :) So I changed the next.config.js to server and then it all works without a server.js file.

showing Failed to find package.json. Node.js may have issues starting. Verify package.json is valid or place code in a file named server.js or app.js

showing Failed to find package.json. Node.js may have issues starting. Verify package.json is valid or place code in a file named server.js or app.js. when deploying node js file in AWS ELASTIC BEANSTALK cloud, what are the reasons to showing that message and how to resolve that issue.
There can be multiple reasons. Can you please check the logs generated in Beanstalk.
The way how you have zipped the package can cause an error. Make sure, you go inside the source directory and zip the files and folders, instead of zipping the single folder itself.
Secondly, the way how you have created the app.js or index.js.
The Nodejs versions can be another reason.
The below approach worked for me 2 years back when I started using Beanstalk for my Nodejs applications.
Also, I would recommend deploying a sample nodejs application from this link and progressing from there. https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/nodejs-getstarted.html

Serverless NodeJS / Native node_modules

I'm having an issue getting a node module to load in AWS Lambda using the Serverless Framework. One of my node packages uses native libraries so I used an EC2 to create the node_module folders and then copied them to my Serverless project. Everything works if I manually zip the project and upload to AWS Lambda but if I use serverless deploy without an artifact specified, I get an error about the module (specifically: ELF file's phentsize not the expected size regarding a .node file)
I've tried adding excludeDevDependencies: false which makes the deployment larger but still gives me the error. Currently, it only works if I zip the contents of the project folder and specify that file as the artifact to upload. Is there a different way to get a node module with native bindings to deploy with Serverless?
UPDATE: After turning off the exclusion of dev dependencies, packaging using serverless package and examining the expanded zip file serverless creates, I discovered the file sizes of the .o and .a files are different in the packaged version as compared to the original. Is this normal?
I ran into this problem and did some digging. It turns out that v1.21.0 of serverless broke packaging of binaries.
https://forum.serverless.com/t/serverless-1-21-0-breaks-sharp-library/2606
https://github.com/serverless/serverless/issues/4182
The recommended fix is to upgrade to v1.21.1.
Since Lambda runs on a Linux container, you should be running serverless deploy from a Linux machine.
This way, your native modules will be compiled for your target architecture which is Linux.
To check the deployment package that serverless creates, you can use sls package or sls deploy --noDeploy (for older versions), and inspect the .serverless directory that it creates. You'll see a zip file in here, extract its contents, and test the code from there.
If the contents of this zip is not what you expect (not the same as when you manually copy them), then maybe something is wrong with your file structure and/or serverless.yml.

Bundle Node.js dependancies in AWS Zip

Im creating a lambda function using Node.js, and Im looking to bundle the dependencies into my zip to be used in my Lambda function in AWS. Specifically Im using the ffmpeg library, and I've got it running locally using npm. Is there a way that I can bundle this dependency with the .zip file that I upload so that I don't have to configure the dependency in AWS?
Not only is there a way to bundle this into your deployment package, but you have to do it this way. Lambda functions can't download dependencies.
Here's the documenation: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html
Also you might want to look at this project: https://github.com/binoculars/aws-lambda-ffmpeg and possibly this thread: https://forums.aws.amazon.com/thread.jspa?messageID=680948 for more specific information about running ffmpeg on Lambda.

Resources