-
-
Notifications
You must be signed in to change notification settings - Fork 376
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
Support CommonJS (again) #1233
Comments
We have no plans to re-induce cjs support. However, the nodejs team may have a solution for you in version 22 with the new |
It just landed in Node 20.17 https://nodejs.org/en/blog/release/v20.17.0 |
@willfarrell It doesn't seem to help. Running: node --experimental-require-module node_modules/.bin/jest And getting the same error:
And import middy from '@middy/core'; NodeJS: 20.17.0
module.exports = (testsType) => ({
preset: "ts-jest",
testEnvironment: "node",
modulePathIgnorePatterns: ['dist'],
testMatch: ['<rootDir>/**/*.test.ts'],
resetMocks: true,
transform: {
'^.+test/.*\\.ts$': ['ts-jest', {
tsconfig: "<rootDir>/tsconfig.json",
}]
},
});
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"lib": ["ES2019", "es2020"],
"allowJs": true,
"rootDir": "./",
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"incremental": true
},
"include": ["src/**/*", "test/**/*"],
"exclude": ["node_modules/**/*"]
} |
Thanks for testing. Based on the node release notes this should have allowed middy to be imported using require based on how it's currently configured. I ran a quick test to confirm: const { default: middy } = require('@middy/core')
const baseHandler = async (event, context, { signal }) => {
console.log('success')
}
middy(baseHandler)() node --experimental-require-module index.cjs Which, as expected, passed. This leads me to believe that there is an issue somewhere in your build process, likely typescript. I would recommend opening an issue for typescript to support transpiling to vanilla CommonJS so Edit: We might need to push a small config change (was modifying too many files at once). Looks like the following is needed in the package.json:
|
@willfarrell This is happening when running jest, which is known not to support ESM properly. Using Adding the
Given how fundamental both Typescript and Jest are for nodejs codebases, I would highly encourage making middy compatible with them; otherwise, I fear devs would abandon middy. We are aware of the exclusive ESM feature of running top-level await, and how it can help with cold starts; however, this is only relevant to provisioned concurrency, which is a very costly solution, especially with non-consistent traffic. |
Thanks for the additional testing. I know Jest has been working on ESM support, curious how close they are.
In my experience this is not true. The platform I maintain has a high cold start hit rate (>80%), migrating to ESM had a notable improvement for us at the time. We don't use provisioned concurrency because, as you mentioned, it is a very costly solution. We do take advantage of the |
@willfarrell It's strange that you saw any change without provisioned concurrency. Without provisioned concurrency, the top-level code of the lambda will start running only on the first request to the container, so it doesn't really matter if it runs in the top-level or inside the handler itself. Maybe there's a general improvement of imports/requires, but I haven't seen any post about it from AWS. |
Closing, 6.0.0-alpha.0 is now released. https://github.com/middyjs/middy/blob/release/6.0.0/website/docs/upgrade/5-6.md#notable-changes |
Please bring back support for CommonJS.
We have a lot of serverless projects (dozens) in NodeJS. They are all written in CommonJS.
There is very little chance that we will migrate to ESM, since it will require a lot of work. Most notably, we rely heavily on Jest, which doesn't support ESM properly.
If Middy won't support CommonJS, we may have to migrate away from it at some point.
The text was updated successfully, but these errors were encountered: