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

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant