5

I have the following issue... turns out I cannot test locally my aws lambda function over serverless-offline plugin in serverless framework for nodejs. Cannot do a simple GET request.

I am able to deploy to aws lambda with serverless deploy command, but in order for better development experience of the team we need to deploy locally with serverless-offline, I followed this guide https://fauna.com/blog/develop-using-serverless-offline, but got stuck when I do a simple get request after running successfully serverless offline command. This I get when running serverless offline successfully serverless offline command output

But when I do a get request to the link http://localhost:3000/ through postman I get the following error

get request error

It basically says cannot find node:url module but I did npm install url, but still same error throwing. I will insert the project tree and files below:

serverless hello world tree structure

handler.js

//"use strict";

console.log("AJAJAJAJAJAJAJAJAJAAJAJ");

console.log("Printing module");
console.log(module.exports);

module.exports.hello = async (event) => {
  console.log("whaaaaat");
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Go Serverless v3.0! Your function executed successfully!",
        input: event,
      },
      null,
      2
    ),
  };
};

serverless.yml

org: ortizjorge97
app: aws-node-http-api-project
service: aws-node-http-api-project
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs14.x

plugins:
  - serverless-offline
  - serverless-bundle
  - serverless-dotenv-plugin

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /
          method: get

package.json

{
  "name": "aws-node-http-api-project",
  "version": "1.0.0",
  "description": "<!-- title: 'AWS Simple HTTP Endpoint example in NodeJS' description: 'This template demonstrates how to make a simple HTTP API with Node.js running on AWS Lambda and API Gateway using the Serverless Framework.' layout: Doc framework: v3 platform: AWS language: nodeJS authorLink: 'https://github.com/serverless' authorName: 'Serverless, inc.' authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4' -->",
  "main": "handler.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "serverless-bundle": "^5.5.0",
    "serverless-dotenv-plugin": "^4.0.2",
    "url": "^0.11.0"
  },
  "devDependencies": {
    "serverless-offline": "^10.2.0"
  }
}

I am newbie on aws stuff, so I dont know what could be happening.

I am using

  • node 14.17.2
  • MacOS Monterey m1 processor
Jorge Ortiz
  • 87
  • 2
  • 4
  • what do you get when you try to require the url from the nodejs cli. And could you please check whether the url module exists in the node_modules/ directory? – ysfiqbl Sep 23 '22 at 15:29

4 Answers4

0

You need to install the actual serverless package itself as a dev dependency. Try this:

npm i -D serverless

Noted: remember to check the documentation of serverless-offline to know find the supported version of serverless package.

0

I fount in too node 14.17.3, framework 3.25.0 (local), plugin 6.2.2, SDK 4.3.2

I can workaround fixing by change require to import in UserFunction.js

img.png

reacter
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '22 at 12:34
0

set your node version to 16.17.0 or higher, and install serverless as a dev dependency

0

This happens because your version of Node 14.17.2 does not support node:url package anymore, as node:url was deprecated and removed.

Either downgrade your Node version to 12.x or downgrade your serverless-offline package to ^8.8.1

Vlado Tesanovic
  • 6,369
  • 2
  • 20
  • 31