-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[Feature] Add a way to handle CJS/ESM module hell #23662
Comments
@segevfiner Thank you for the issue! The situation with CJS/ESM is not ideal indeed. We have recently made some changes to make things easier, but there is definitely room for improvement.
Overall, Playwright mostly assumes it executes the Node.js program, not the frontend code that is only written to be consumed by carefully configured bundlers. However, we are open to accommodate some setups. If you have some specific asks, let us know and we'll take a look. Otherwise, the landscape is very diverse, and we won't be able to universally handle it in a single way. |
So there is no way to resolve lodash-es esm support ?! ㅠ |
quick google says What we did in our repo is that we created a pnpm monorepo, and have separate package just for playwright tests that we made as a module, so we can benefit from all of the esm features |
It's actually vanilla If we had a flag like in vitest to force Playwright to transpile it instead of importing it directly |
So to summarize:
And that's why we can't have nice things... |
@segevfiner I've tried your sample repository, and changing
What forces the ESM mode for you? I don't see |
Not in this repo. But I tried to enable ESM mode for some other project because some other package than lodash-es is ESM only in that project. Basically when I have one package that has problem with named exports in ESM mode, and another one that is ESM only, you have an issue with getting either mode working properly. |
I have hit another issue where Playwright fails to import
|
That makes no sense. The entire point of Playwright is to write unit tests for browser code, that's why Playwright runs the tests in a headless browser. So it should not be assuming that it is executing Node.js code! Playwright should be following bundler conventions, not Node.js conventions, because it is running browser code, not Node.js code. And browser code uses bundlers. If somebody wants to unit test Node.js code, they shouldn't be using Playwright in the first place, because Playwright runs the test code in the browser, not Node.js. So it makes no sense for Playwright to be catering to the Node.js behavior. |
that's not exactly true |
@MindaugasMateika Could you please explain more? Under what circumstances does Playwright run the user's unit test code in Node.js? |
I think pretty much whole code runs on Node.js process and commands are sent to browsers over web-sockets to drive the automation on browser side. So the tests code is more like Node.js program than something else. |
I have a playwright + lighthouse 10 + custom Audit setup. Lighthouse takes the module name for the module that contains the CustomAudit class & imports it. My "CustomAudit" class extends Audit defined by lighthouse (which is an esm module). I can get this to work if I define CustomAudit in a .mjs file, where I can statically import { Audit } from 'lighthouse'; and derive from it, eg: export default class CustomAudit extends Audit. But, if I try to instead use an .mts file and write customAudit.mts in typescript, I get "Error: Cannot find module '.../customAudit.mts.js" I believe if the compiled .mts compiled to a .mjs instead of a .mts.js this approach could work, as node would be able to run the .mjs. Hope this makes sense :) |
Additional issues:
|
I have same issues in ESM mode: could not import
dayjs plugins does not work when importing without
If I decide to use ESM mode for entire project (e.g.: add |
You can use
For commonjs code |
Then I get such error in client side code
|
This wouldn't be the ideal solution for everyone, but since the Bun runtime handles CJS/ESM resolution under the hood, I would imagine that integrating the Bun runtime to run Playwright tests would render this a non-issue for those that are willing to use it (instead of Node.js). However, that is easier said then done, especially as Playwright currently has Node.js version checks baked in. Relative issues from both communities (PW & Bun):
EDIT: looks like this is on the way! Jarred (Bun creator) created this PR 30min ago: #28875 |
is https://github.com/kolodny/safetest a possible solution to this issue? im still trying to figure things out but maybe we can make it work |
This is becoming a catch-22 problem for me because of this issue in a prettier plugin. I need the type assertion so my Playwright e2e tests fail, but I then my lint/formatting tests fail. Is there absolutely any way, without removing Edit: Was able to work around this by just changing my Vite config file to |
This has rendered our usage of Playwright's component testing unusable, since we're using Is there nothing that can be done? |
Can we get some answers for this? We're stuck with barebones Playwright tests because we can't actually execute any backend code in our E2E tests, which is often necessary to set up state for tests. I'd hate to go back to Cypress at this point. Currently dealing with
And this is after doing all kinds of ESM changes in our monorepo. |
@divmgl you can try: import file from './file.json' assert { type: 'json' }; |
? I don't have access to this library nor do I use it. Seems to be in the resolution path for one of our packages. We've decided to stop using Playwright in this manner. |
Hi! I'm facing a similar error. We have two internal libraries (for API calls) that use ESNext. I updated my Playwright framework, and I'm not able to run my tests that use the code of these libraries. I can't see in the UI Playwright mode those tests that use the logic of these two libraries. If I attempt to execute one of these tests through the terminal, I get an error complaining about one of the files that come from these libraries. I attached the images of the existing configuration I use. Through UI mode.- Through terminal.- I added the file extensions in my imports, and continues not working. I removed the Is there a solution for these problems? |
It looks there will soon be a patch incoming for Node.js that fixes the ESM/CJS interoperability. May be worth keeping a watch on this. nodejs/node#51977 |
This is causing problems for us as well with |
Is there really no solid solution for this atm? 😭 |
I'm getting exactly the same behavior as @ArCiGo. Did you ever figure it out? |
For our internal project I wrote a custom patch for Here's the patch file:
You can apply it using
if you want to mock certain files you can point to a mock file instead like:
if you want to ignore certain files (in my case I want to ignore certain files from barrel files 'cause they import Angular down the line):
|
has anyone tried with node 22? it seems you can require esm now |
Is there any link to repo where you have manually configured test runners to transform modules that have trouble importing in either CJS or ESM mode? |
…#31285) Historically, this plugin was important to translate dynamic imports into require calls so that we can intercept them and transpile. This is not needed anymore with ESM loader enabled by default, so we can avoid this transformation and support dynamic imports of ESM-only packages/files. Fixes #17075, fixes #23255, fixes #31140, references #23662.
I can confirm, running it with |
Consider me a newbie. How can this fix or flag be applied in a Playwright environment? |
Overview The team have discussed the situation with the CJS/ESM modules. We believe that Playwright now aligns with Node.js ecosystem, loading CJS/ESM in the same way as pure Node.js would do, with an exception of supporting CJS/ESM interoperability Some developers are struggling with an issue where the test project uses both esm-only and cjs-only packages, either directly or indirectly through importing the app sources. Unfortunately, the Node.js ecosystem has not fully solved this problem yet. However, there are significant developments in the area, for example Loading ECMAScript modules using For now, you can try the experimental Node.js flag. Note that you should not have # Linux / macOS
NODE_OPTIONS=--experimental-require-module npx playwright test
# Window
set NODE_OPTIONS=--experimental-require-module
npx playwright test Still have a problem? If you encounter a specific ESM or CJS loading problem that is not related to using both esm-only and cjs-only packages in the same project (see above for details), please file a separate issue by following the "Bug Report" template. |
With the current sorry state of the JavaScript ecosystem, an ongoing struggle is the CJS/ESM modules hell. Whereby, importing CJS from ESM, or ESM from CJS. is troublesome in plain Node, and only really works well in bundlers. Sadly, many things will force you to use CJS (Due to not supporting ESM), and many many modules are ESM only, or otherwise CJS with no compatibility for Node's ESM named import rules, or file extension/no directory imports requirements.
An example is this https://github.com/segevfiner/playwright-ct-vue-on-issue/blob/main/src/components/__tests__/esm-hell.pw.ts
This test imports a TypeScript file that imports
lodash-es
which is ESM only. This fails in Playwright without"type": "module"
set as you can't require an ESM module. With"type": "module"
, you then hit a truck load of other issues with the poor state of Node's native ESM mode. All imports in the entire code base, including in dependencies, must be adjusted to include file extensions, which TypeScript doesn't seem to check for, except with"module/moduleResolution": "node16"
but that mode is troublesome with bundlers. Further more, many CJS modules are not compatible with named imports from ESM as they don't define their exports in a statically analyzable way, this again results in aggravating failures when trying to import them in Node's ESM mode.To handle this hellish nightmare, I found myself having to manually configure test runners to transform modules that have trouble importing in either CJS or ESM mode, depending on what the test runner uses, and to use tsx to run scripts as it transforms the modules using esbuild to the proper format before Node.
It would be very helpful if
@playwright/test
like Jest and Vitest, had a way to transform such problems modules too. When running in CJS mode, transform ESM modules to CJS, and vice versa when running in ESM mode, so that we are not limited by the annoying limitations of Node, which bundlers are not limited by, and can focus on actually writing code rather than fighting in the unholy CJS vs ESM purity war.In Jest this is done by configuring a negative lookahead regexp in
transformIgnorePatterns
, and configuring your transform to perform the needed conversion, e.g. Babel's ESM to CJS.In Vitest you sometimes need to configure
deps.inline
, including all modules that lead to such a problem module, as Vitest doesn't transform after an inner import/require of a non-transformed module. (It's not a require/loader hook like Jest ATM).The text was updated successfully, but these errors were encountered: