I'm trying to run serverless-offline using serverless-middleware plugin and I'm getting error during execution.
Current Behavior
According to serverless-middleware documentation:
Once serverless-middleware is installed you can set the function.handler property to an array. Each middleware handler can be a string (like a standard handler would be) or an object containing the properties then and/or catch.
So I defined the function.handler (getAllUsers.handler) in my case, as an array.
When I'm running sls deploy --stage dev command, everything works as expected. The deployment works great, and the middleware functionality works as expected.
But, When I'm running sls offline start -s dev command, I'm getting the following error:
> sls offline start -s dev
Serverless: Configuration warning at 'functions.getAllUsers.handler': should be string
Serverless:
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:
Serverless Error ---------------------------------------
No matching handler found for './middleware.middlewareFunc,./user' in 'C:\Users\avishay_a\WebstormProjects\middleware-test'. Check your service definition.
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com
Your Environment Information ---------------------------
Operating System: win32
Node Version: 12.13.0
Framework Version: 2.7.0
Plugin Version: 4.1.0
SDK Version: 2.3.2
Components Version: 3.2.1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] serverless-offline-dev: `sls offline start -s dev`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] serverless-offline-dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\avishay_a\AppData\Roaming\npm-cache\_logs\2021-01-19T07_47_15_850Z-debug.log
Sample Code
All files also available at this GitHub Repo
service: my-service
plugins:
- serverless-webpack
- serverless-offline
- serverless-middleware
provider:
runtime: nodejs12.x
stage: dev
functions:
getAllUsers:
handler:
- ./middleware.middlewareFunc
- ./user.getFunc
events:
- http:
method: GET
path: users
'import {APIGatewayProxyHandler} from 'aws-lambda';
import 'source-map-support/register';
export const middlewareFunc: APIGatewayProxyHandler = async (event, _context) => {
try {
console.log('middlewareFunc');
return 'Message from middleware';
} catch (e) {
return e;
}
};
import {APIGatewayProxyHandler} from 'aws-lambda';
import 'source-map-support/register';
export const getFunc: APIGatewayProxyHandler = async (event, _context) => {
try {
console.log('user getFunc')
return {
statusCode: 200,
body:JSON.stringify({
message: _context.prev,
data:[
{
name:'John',
gender:'male'
},
{
name:'Doe',
gender:'male'
}
]
})
};
} catch (e) {
return e;
}
};
Expected behavior/code
The expected behavior should be the same as deployment without using serverless-offline plugin.
e.g - real deployment log:
> sls deploy --stage dev
Serverless: Configuration warning at 'functions.getAllUsers.handler': should be string
Serverless:
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:
Serverless: Middleware: setting 2 middlewares for function getAllUsers
Serverless: Bundling with Webpack...
Time: 722ms
Built at: 01/19/2021 09:45:36
Asset Size Chunks Chunk Names
.middleware/getAllUsers.js 1.67 KiB 0 [emitted] .middleware/getAllUsers
.middleware/getAllUsers.js.map 7.43 KiB 0 [emitted] [dev] .middleware/getAllUsers
Entrypoint .middleware/getAllUsers = .middleware/getAllUsers.js .middleware/getAllUsers.js.map
[0] external "source-map-support/register" 42 bytes {0} [built]
[1] ./.middleware/getAllUsers.ts + 2 modules 1.42 KiB {0} [built]
| ./.middleware/getAllUsers.ts 558 bytes [built]
| ./middleware.ts 247 bytes [built]
| ./user.ts 642 bytes [built]
Serverless: WARNING: Could not determine version of module source-map-support
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: source-map-support
Serverless: WARNING: Could not determine version of module source-map-support
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service middleware-test.zip file to S3 (290.43 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: middleware-test
stage: dev
region: us-east-1
stack: middleware-test-dev
resources: 11
api keys:
None
endpoints:
GET - https://nfd5plc9p4.execute-api.us-east-1.amazonaws.com/dev/users
functions:
getAllUsers: middleware-test-dev-getAllUsers
layers:
None
Environment
serverless version: v2.7.0serverless-offline version: v6.8.0node.js version: v12.13.0
OS: Windows 10
+1
something that made it work is using serverless offline start instead of simply serverless offline. Im assuming start is a legacy execution.
something that made it work is using
serverless offline startinstead of simplyserverless offline. Im assumingstartis a legacy execution.
I think the command used in the original question is with “offline”...
didn’t seems to do the trick.
In what version did it help?
We are also looking for a solution
something that made it work is using
serverless offline startinstead of simplyserverless offline. Im assumingstartis a legacy execution.
Worked for me, thanks!
serverless versions:
"serverless": "^2.28.0"
"serverless-middleware": "0.0.14",
"serverless-offline": "^6.8.0"
Most helpful comment
something that made it work is using
serverless offline startinstead of simplyserverless offline. Im assumingstartis a legacy execution.