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

[BUG] Regression in 1.27 for ESM file extensions #18077

Closed
drwpow opened this issue Oct 13, 2022 · 5 comments · Fixed by #18219
Closed

[BUG] Regression in 1.27 for ESM file extensions #18077

drwpow opened this issue Oct 13, 2022 · 5 comments · Fixed by #18219
Assignees

Comments

@drwpow
Copy link

drwpow commented Oct 13, 2022

Context:

  • Playwright Version: 1.27.1
  • Operating System: macOS
  • Node.js version: 18.10
  • Browser: N/A
  • Extra: N/A

Code Snippet

Trying to import test-utils.ts in a Playwright test:

// App.test.ts
import { setupTest } from './test-utils.js';

// Error: Cannot find module './test-utils.js'

Describe the bug

When trying to import .ts files, ESM requires an extension. This was working in version 1.26. However, when upgrading to 1.27, the “Cannot find module” error occurs. Downgrading back to 1.26 again fixes the problem and the file is imported just fine.

Worth highlighting that test-utils.ts (TypeScript) is the extension, not .js, but following convention .js is preferred.

Also worth noting that my repo is not "type": "module" which may be causing an issue (but we are using ESM throughout and are using "module": "ESNext" in TSConfig).

@pavelfeldman
Copy link
Member

pavelfeldman commented Oct 19, 2022

Things work for me using exact import from your example.

Also worth noting that my repo is not "type": "module" which may be causing an issue (but we are using ESM throughout and are using "module": "ESNext" in TSConfig).

Note that since you do not specify "type": "module", you are running in the CJS mode, tsconfig has no control over the Node runtime. So your TS imports are converted into the requires. It also assumes that test-utils.js is a CJS file. You should pick the site you are on: ESM or CJS and be consistent with it.

@drwpow
Copy link
Author

drwpow commented Oct 19, 2022

Yes we probably should change to { "type": "module" } at the package level. But I’m still noticing a behavioral change where from 1.26 to 1.27 that same import either works or doesn’t. That seems like unexpected behavior? That’s more the issue here.

I do have workarounds such as using require(), or omitting the extension, that seem to work in 1.27. But I guess I’m wondering why the import logic changed between 1.26 and 1.27. I don’t believe it has anything to do with tsconfig.

@pavelfeldman
Copy link
Member

There were subtle changes to the resolver/loader, they could have affected things. In either case, if you have a small repro where import { setupTest } from './test-utils.js'; does not work, could you share it with us? It should work and it does work for me locally on a small example.

@drwpow
Copy link
Author

drwpow commented Oct 20, 2022

Sure! I was able to create a fairly-simple repro: https://github.com/drwpow/playwright-import-repro/.

Curious if this is just something weird on my machine or if you see the error too here.

@pavelfeldman
Copy link
Member

Awesome, thanks, I can repro it. I just could not add 2 + 2 in your original description - now that I read it, it explains the use case very well.

// example.test.ts
import { gimmeAOne } from "./playwright-utils.js";
// playwright-utils.ts
export function gimmeAOne() {
  return 1;
}

does not work in Playwright v1.27 in non-ESM mode.

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

Successfully merging a pull request may close this issue.

3 participants