juanjoDiaz/serverless-middleware

not working since 3.2.0

Closed this issue · 5 comments

upgraded to 3.2.0 from 3.1.0, and not working since, throwing errors like

  in ./.middleware/SERVICENAME-dev-validateUnit.ts 4:0-119
  Module not found: Error: Can't resolve '../PATH/validate-unit.js' in '/Users/zooli/SERVICEDIR/.middleware'
  resolve '../PATH/validate-unit.js' in '/Users/zooli/SERVICEDIR/.middleware'
    using description file: /Users/zooli/SERVICEDIR/package.json (relative path: ./.middleware)
      using description file: /Users/zooli/SERVICEDIR/package.json (relative path: ./PATH/validate-unit.js)
        no extension
          /Users/zooli/SERVICEDIR/PATH/validate-unit.js doesn't exist
        .ts
          /Users/zooli/SERVICEDIR/PATH/validate-unit.js.ts doesn't exist
        .js
          /Users/zooli/SERVICEDIR/PATH/validate-unit.js.js doesn't exist
        as directory
          /Users/zooli/SERVICEDIR/PATH/validate-unit.js doesn't exist

it's a TS project, node 18, serverless 3.33.0

i had the same issue, make sure if you're using type script and have serverless-plugin-typescript, move middleware plugin before typescript plugin, same for tscpaths

here is my serverless.yml before change

... other configs
plugins:
  - serverless-dotenv-plugin
  - serverless-plugin-typescript
  - serverless-tscpaths
  - serverless-middleware
... other configs

image

after change

... other configs
plugins:
  - serverless-dotenv-plugin
  - serverless-middleware
  - serverless-plugin-typescript
  - serverless-tscpaths
... other configs

image

Hi,

sorry for the slow response.
The main change introduced in v3.2.0 was to ad the file extension to all files using .js.
Typescript does not modify imports and this is the recommended way.

Regarding @rezaerami suggestion, the order is important indeed.
If typescript is used before middleware, then everything is compiled by the typescript plugin and then the middlewares are chained together to create the final handler.
If middleware is used before typescript, then a typescript handler is creating importing all the middlewares and then the typescript function transpiles it to javascript.
I always try to put the middleware plugin before any kind of transpilation plugin or any plugin that changes the code

If you can create a sample project to reproduce the issue, I will fix it :)

@zoli-kasa, please read #52 as it provides the solution to your problem.

The issue is related to ts-loader & webpack config.

thanks @juanjoDiaz, it worked!

it'd be nice if you could add this to breaking changes or the changelog.