juanjoDiaz / serverless-middleware

Serverless plugin to allow middleware handlers configured directly in serverless.yaml
MIT License
18 stars 11 forks source link

ERR_MODULE_NOT_FOUND using serverless-offline #48

Closed web-fu closed 1 year ago

web-fu commented 1 year ago

I want to test my application locally so I use serverless-online. Without attaching a middleware, any endpoint work fine, but when I attach the middleware it fails to find the module built:

here there is the releant part of my serverless.yaml file

  processGet:
    handler: src/process/getOne.handler
    events:
      - http:
          path: /${env:APP_DOMAIN}/process/{pid}
          method: GET
          cors: true
          documentation:
            summary: 'Retrieves one process by its unique identifier'
            pathParams:
              - name: pid
                description: 'The process id'
                in: path
                schema:
                  type: 'string'
                  format: 'uuid'
            methodResponses:
              - statusCode: 200
                responseBody:
                  description: 'The process with that identifier'
                responseModels:
                  application/json: 'Process'
              - statusCode: 400
                responseBody:
                  description: 'Bad Request'
                responseModels:
                  application/json: 'BadRequest'
              - statusCode: 404
                responseBody:
                  description: 'Not Found'
                responseModels:
                  application/json: 'ErrorResponse'
              - statusCode: 500
                responseBody:
                  description: 'Internal Server Error'
                responseModels:
                  application/json: 'ErrorResponse'
          private: true
    middleware:
      pre:
        - src/middleware/logger.handler

I noticed that running npm install && serverless offline works fine until I call my endpoint

✖ Unhandled exception in handler 'processGet'.
✖ Runtime.ImportModuleError: Error: Cannot find module 'fa-api-proxy-dev-processGet'
  Require stack:
  - <omitted>/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js
      at _loadUserApp (<omitted>/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js:310:15)
      at async module.exports.load (<omitted>/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js:341:21)
      at async InProcessRunner.run (<omitted>/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js:41:21)
      at async MessagePort.<anonymous> (<omitted>/node_modules/serverless-offline/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js:24:14)
✖ Error: Cannot find module 'fa-api-proxy-dev-processGet'
  Require stack:
  - /<omitted>/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js

The module is correctly created in the directory, but seems that cannot be loaded correctly.

juanjoDiaz commented 1 year ago

Hi,

Sorry for the delay answering. I need a sample project to be able to reproduce the issue and give you a solution.

web-fu commented 1 year ago

I removed the middleware from my project without saving any commit :( I created a new project with all the packages I used, but now middleware don't transpile. Any help would be usefull: https://github.com/web-fu/serverless-middleware

juanjoDiaz commented 1 year ago

This issue when off my radar. Sorry for slow response.

serverless-offline doesn't trigger it's own events consistently. I'll fix that on my side. In the meantime, you can workaround running serverless offline start.

Also, your path to logger is wrong. You should use:

    middleware:
      pre:
        - .middleware/logger.logMiddleware