Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error since upgrading to middy 3.x #182

Open
TPME opened this issue Oct 12, 2022 · 2 comments
Open

Getting error since upgrading to middy 3.x #182

TPME opened this issue Oct 12, 2022 · 2 comments

Comments

@TPME
Copy link

TPME commented Oct 12, 2022

Hi, I just upgraded middy to version 3 as well as your plugin. Unfortunately I get the following error now:

2022-10-12T15:37:36.879Z	undefined	ERROR	Uncaught Exception 	
{
    "errorType": "TypeError",
    "errorMessage": "cache is not a function",
    "stack": [
        "TypeError: cache is not a function",
        "    at applyCacheOption (webpack-internal:///3705:180:49)",
        "    at middify (webpack-internal:///3705:83:28)",
        "    at eval (webpack-internal:///3381:6:46)",
        "    at Object.3381 (/var/task/src/modules/posts/postsRoute.js:212:1)",
        "    at __webpack_require__ (/var/task/src/modules/posts/postsRoute.js:432:42)",
        "    at /var/task/src/modules/posts/postsRoute.js:443:37",
        "    at Object.<anonymous> (/var/task/src/modules/posts/postsRoute.js:448:12)",
        "    at Module._compile (node:internal/modules/cjs/loader:1105:14)",
        "    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)",
        "    at Module.load (node:internal/modules/cjs/loader:981:32)",
        "    at Function.Module._load (node:internal/modules/cjs/loader:822:12)",
        "    at Module.require (node:internal/modules/cjs/loader:1005:19)",
        "    at require (node:internal/modules/cjs/helpers:102:18)",
        "    at _tryRequireFile (file:///var/runtime/index.mjs:869:37)",
        "    at _tryRequire (file:///var/runtime/index.mjs:919:25)",
        "    at _loadUserApp (file:///var/runtime/index.mjs:945:22)",
        "    at Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:976:27)",
        "    at start (file:///var/runtime/index.mjs:1137:42)",
        "    at file:///var/runtime/index.mjs:1143:7",
        "    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)",
        "    at async Promise.all (index 0)",
        "    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)",
        "    at async loadESM (node:internal/process/esm_loader:88:5)",
        "    at async handleMainPromise (node:internal/modules/run_main:61:12)"
    ]
}

What gives? I have not changed the way I use your plugin, which is basically:

import * as cache from "@schibsted/middy-caching-headers";

let middifiedHandler = middy(handler);

const cacheTime = 10 * 60;

middifiedHandler = middifiedHandler.use(
  cache({
    success: {
      directive: `public,stale-while-revalidate=${
        cacheTime / 10
      },stale-if-error=${cacheTime}`,
      serverTime: cacheTime,
      clientTime: cacheTime,
    },
    errors: {
      default: {
        directive: `public,stale-while-revalidate=${
          cacheTime / 10
        },stale-if-error=${cacheTime}`,
        serverTime: cacheTime / 10,
        clientTime: cacheTime / 10,
      },
      404: {
        directive: `public,stale-while-revalidate=${
          cacheTime / 10
        },stale-if-error=${cacheTime}`,
        serverTime: cacheTime,
        clientTime: cacheTime,
      },
    },
  })
);

@TPME
Copy link
Author

TPME commented Oct 12, 2022

Update: It looks like the error is only thrown when I use the following tsconfig.json:

{
    "compilerOptions": {
      "lib": ["es2021"],
      "module": "commonjs",
      "target": "es2021",
      "strict": false,
      "allowJs": true,
      "esModuleInterop": true,
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true,
      "moduleResolution": "node",
      "removeComments": true,
      "sourceMap": true,
      "outDir": "lib",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "allowSyntheticDefaultImports": true
    },
    "include": ["./src/**/*"],
    "exclude": [
      "node_modules/**/*",
      ".serverless/**/*",
      ".webpack/**/*",
      "_warmup/**/*",
      ".vscode/**/*"
    ]
  }

I wonder which part exactly is causing it?

@TPME
Copy link
Author

TPME commented Oct 12, 2022

Update. It's "module": "commonjs", that is causing it. Removing that line solved it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant