Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Error while importing ES Module in commonjs via Dynamic import #920

Open
testgitdl opened this issue Aug 31, 2022 · 0 comments
Open

Error while importing ES Module in commonjs via Dynamic import #920

testgitdl opened this issue Aug 31, 2022 · 0 comments

Comments

@testgitdl
Copy link

We have an inhouse developed module that we need to use in a commonjs project. In case in main.page.ts we import login.page than an "Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:_AutomationProjects\CTA\Playwright\dist\page-objects\esm2020\lib\login.page' imported from C:_AutomationProjects\CTA\Playwright\dist\page-objects\esm2020\lib\main.page.mjs" appears. It appears that dynamic import is not working in this case..otherwise no problem

Contents of the inhouse developed module:

login.page.ts

export class LoginPage {
  public page: Page;
  constructor(page: Page) {
    this.page = page;
  }
}

main.page.ts

import { LoginPage } from "./login.page";

export class MainPage {
  public page: Page;
  public login: LoginPage;
  constructor(page: Page) {
    this.page = page;
    this.login = new LoginPage(page);
  }
}

Then this project is installed in another one that is commonjs and to use it we make use on dynamic imports

main.cjs

mainPage = new (await import('../../dist/page-objects/esm2020/lib/main.page.mjs')).MainPage(page);

If in main.page.ts we remove the login.page import that everything is working.

How can we import es modules in commonjs in this case? Cannot move away from commonjs as the test tool does not know how to handle ESM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant