Search code examples
typescriptplaywrightsveltekit

Cannot find module X imported from Y - Playwright with Typescript


I just installed a new version of SvelteKit with Playwright and TS. I am trying to import a regular .ts file inside my A.spec.ts file.

tests
  A.spec.ts
  B.ts

a.spec.ts

import { test, expect } from "@playwright/test";
import { mymodule } from './B';
...

I get the following error when I run npm run test (playwright test):

Error: Cannot find module 'my\imported-module\path\tests\B' imported from A.spec.ts

VS Code does not show any errors with my code.

How can I import a regular TS file into my playwright TS testing script?

J

UPDATE - Here is a basic Github Repo


Solution

  • Change:

    import { my_module } from './utils';

    To:

    import { my_module } from './utils.js';

    This an issue with ESM in node and happens when using "type": "module".

    imports will fail in ES modules because relative import paths need to use extensions.
    As a result, it will have to be rewritten to use the extension of the output of foo.ts, so instead we have to import from ./foo.js.

    https://www.typescriptlang.org/docs/handbook/esm-node.html