Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
alexn-s opened this issue · 4 comments
Description
On version 3.0.0 clipboardy crashes with following message:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: c:\Users\...\node_modules\clipboardy\index.js
require() of ES modules is not supported.
require() of c:\Users\...\clip-issue\node_modules\clipboardy\index.js from c:\Users\...\clip-issue\src\main.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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from c:\Users\...\clip-issue\node_modules\clipboardy\package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (c:\Users\...\clip-issue\src\main.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Module.m._compile (C:\Users\...\Roaming\npm\node_modules\ts-node\src\index.ts:1295:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\...\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1298:12)
Version 2.3.0 works fine. The code in the linked repo uses regular ts import syntax.
Replicate
Just clone the linked repo, install libs (used yarn) and run the typescript file with ts-node
Link
Please read the release notes.
I would wait upgrading until TS 4.5 I'd put as it has better ESM support.
my bad, sorry. thanks for the info and the awesome library.
For anyone using the latest TypeScript and having this issue, I managed to counter it by doing the following:
- Under
tsconfig.json
, updatemodule
to"Node16"
- On my code, import Clipboardy by using
const clipboardy = (await import("clipboardy")).default
It's a bit of a hack, but seems to be the only way since TypeScript doesn't seem to recognize ESM correctly and tries to load them with require
instead of import
.
Why was this closed? It's still not working properly