简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM