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.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module @preact\preset-vite when scaffolding vanilla preact

See original GitHub issue

When using npm init vite with the preact framework and preact (vanilla js) variant, I get the following error when starting the development server after npm install:

error when starting dev server:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\Javascript\starter-vite-preact\node_modules\@preact\preset-vite\dist\index.js
require() of ES modules is not supported.
require() of E:\Javascript\starter-vite-preact\node_modules\@preact\preset-vite\dist\index.js from E:\Javascript\starter-vite-preact\vite.config.js 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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\Random42\Javascript\starter-vite-preact\node_modules\@preact\preset-vite\package.json.

    at Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Object.require.extensions.<computed> [as .js] (E:\Javascript\starter-vite-preact\node_modules\vite\dist\node\chunks\dep-36bf480c.js:77286:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (E:\Random42\Javascript\starter-vite-preact\vite.config.js:30:37)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.require.extensions.<computed> [as .js] (E:\Javascript\starter-vite-preact\node_modules\vite\dist\node\chunks\dep-36bf480c.js:77283:20)
    at Module.load (internal/modules/cjs/loader.js:928:32)

Deps versions are

    "preact": "^10.5.13"
    "@preact/preset-vite": "^2.0.0",
    "vite": "^2.5.4"

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
piotr-czcommented, Sep 27, 2021

@marvinhagemeister Thank You!

1reaction
piotr-czcommented, Sep 27, 2021

Same here. The error goes away after adding "type": "module" in package.json, however VSCode started to complain now (Cannot find module ‘@preact/preset-vite’ or its corresponding type declarations.).

There was an attempt to update vite templates (https://github.com/vitejs/vite/pull/4293) but it has been closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ESLint - Error: Must use import to load ES Module
I think the problem is that you are trying to use the deprecated babel-eslint parser, last updated a year ago, which looks like...
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