question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] "Must use import to load ES Module" when importing lodash-es into Playwright test

See original GitHub issue

Update: I created a minimal test repo that reproduces my problem, if that helps. https://github.com/CheapeOne/testapp .

Context:

  • Playwright Version: 1.19.2
  • Operating System: Mac
  • Node.js version: v12.22.9
  • Browser: All
  • Extra:
    • lodash-es v4.17.21
    • typescript v4.4.3
    • yarn v1.22.17

Code Snippet

Sample test file:

// test.spec.ts
import { test } from '@playwright/test';
import { range } from 'lodash-es';

test('example', async ({page}) => {
  await page.goto('https://wikipedia.org');
  console.log(range);
});

Running: npx playwright test --headed

Results in:

Error [ERR_REQUIRE_ESM] [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/asdf/testapp/node_modules/lodash-es/lodash.js
require() of ES modules is not supported.
require() of /Users/asdf/testapp/node_modules/lodash-es/lodash.js from /Users/asdf/testapp/playwright/test.spec.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename lodash.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/asdf/testapp/node_modules/lodash-es/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/Users/asdf/testapp/playwright/test.spec.ts:2:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Module._compile (/Users/asdf/testapp/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Object.newLoader [as .ts] (/Users/asdf/testapp/node_modules/pirates/lib/index.js:141:7)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Loader._requireOrImport (/Users/asdf/testapp/node_modules/@playwright/test/lib/loader.js:269:14)

Describe the bug

My actual problem is I’m trying to import some mocks which themselves use lodash-es, but could reproduce the same error by simply trying to import lodash-es in a test file. This same error seems to happen even if I downgrade lodash-es version. I also tied using PW_EXPERIMENTAL_TS_ESM=1 npx playwright test --headed but the same result.

Here’s my tsconfig.json if that helps. I’m curious if there’s a problem there, playwright’s test runner, or a problem with lodash-es.

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "assumeChangesOnlyAffectDirectDependencies": true,
    "baseUrl": ".",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "esnext"
  }
}

Lastly I’ve tried manually compiling tests (suggested in the docs here) but same result.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CheapeOnecommented, Mar 22, 2022

Oh sweet, and I see you used npm and not yarn, I had included a yarn.lock file but if it’s succeeding with a totally fresh install i think i need to regenerate that. Thanks again for the help, I’ll close this.

1reaction
pavelfeldmancommented, Mar 21, 2022

I don’t see "type": "module" in your test project’s package.json. You can only import modules from modules.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Must use import to load ES module - Stack Overflow
js, I get another error: "Must use import to load ES Module." I also have this as my package.json { "name": "src", "version" ......
Read more >
JS Fix For "SyntaxError: Cannot Use Import Statement Outside ...
"SyntaxError: Cannot use import statement outside a module" is a common Node.js error when working with import to include modules or packages.
Read more >
Configuring Jest
This option tells Jest that all imported modules in your tests should be mocked automatically. All modules used in your tests will have...
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
In ESM scripts, import and export are part of the language; like CJS, they have two different syntaxes for named exports and the...
Read more >
Node : error Must use import to load ES Module - JavaScript
error message > tinder-backend@1.0.0 start D:\tinder\tinder-backend > node server.js internal/modules/cjs/loader.js:1174 throw new ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found