网站搜索

无法找到从 Y 导入的模块 X - 使用 TypeScript 的 Playwright


我刚安装了带有 Playwright 和 TS 的新版本 SvelteKit。我正在尝试在我的 A.spec.ts 文件中导入一个常规的 .ts 文件。

tests
  A.spec.ts
  B.ts

a.spec.ts

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

当我运行 npm run test(playwright test)时,我收到以下错误:

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

VS Code 没有显示我的代码有任何错误。

我如何将常规 TS 文件导入到我的 Playwright TS 测试脚本中?

J

更新 - 这是一个基本的 Github 仓库

答案#1

更改:

import { my_module } from './utils';

收件人:

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

这是 Node 中 ESM 的一个问题,在使用 "type": "module" 时会发生。

在 ES 模块中导入将失败,因为相对导入路径需要使用扩展名。
因此,必须重写以使用 foo.ts 输出的扩展名,所以我们必须从 ./foo.js 导入。

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