Node supports require without an extension, but does not support import without an extension. The built middleware files all have no extension on the imports. This is fine when they're compiled to an ES5 target because they are rewritten to requires, but when targeting ES6 they are not rewritten and remain without an extension. This means running a function in Serverless throws a ERR_MODULE_NOT_FOUND.
It seems that adding .js in the import template path in /src/typescript.js:26 solves this. It may also need adding to /src/javascript.js:26. I'm not sure if this will cause more problems than it solves if people are importing non-js files here (which they shouldn't be as middleware), but changing this line makes everything work for me.
Node supports
require
without an extension, but does not supportimport
without an extension. The built middleware files all have no extension on the imports. This is fine when they're compiled to an ES5 target because they are rewritten torequire
s, but when targeting ES6 they are not rewritten and remain without an extension. This means running a function in Serverless throws a ERR_MODULE_NOT_FOUND.My tsconfig.json looks like this:
And Serverless is targetting nodejs18.x.
It seems that adding
.js
in the import template path in/src/typescript.js:26
solves this. It may also need adding to/src/javascript.js:26
. I'm not sure if this will cause more problems than it solves if people are importing non-js files here (which they shouldn't be as middleware), but changing this line makes everything work for me.