Error [ERR_REQUIRE_ESM]: require() of ES Module /home/sky/Documents/hourlypets/node_modules/tsl-mastodon-api/lib/index.js from /home/sky/Documents/hourlypets/src/mastodon.ts not supported. Instead change the require of index.js in /home/sky/Documents/hourlypets/src/mastodon.ts to a dynamic import() which is available in all CommonJS modules. at require.extensions. [as .js] (/usr/lib/node_modules/ts-node/dist/index.js:851:20) at Object. (/home/sky/Documents/hourlypets/src/mastodon.ts:36:31) at m._compile (/usr/lib/node_modules/ts-node/dist/index.js:857:29) at require.extensions. [as .ts] (/usr/lib/node_modules/ts-node/dist/index.js:859:16) at Object. (/home/sky/Documents/hourlypets/src/bot.ts:13:20) at m._compile (/usr/lib/node_modules/ts-node/dist/index.js:857:29) at require.extensions. [as .ts] (/usr/lib/node_modules/ts-node/dist/index.js:859:16) at phase4 (/usr/lib/node_modules/ts-node/dist/bin.js:466:20) at bootstrap (/usr/lib/node_modules/ts-node/dist/bin.js:54:12) at main (/usr/lib/node_modules/ts-node/dist/bin.js:33:12) at Object. (/usr/lib/node_modules/ts-node/dist/bin.js:579:5) { code: ‘ERR_REQUIRE_ESM’ } I am importing the lib like this
import * as Mastodon from ‘tsl-mastodon-api’;
This is usually a side effect of the uneven adoption of ESM vs CommonJS and mixing of the two. It’s a bit annoying, but, usually fairly easy to work around. But, a lot of it depends on how your project is configured - both the package.json and tsconfig.
Do you have the code available somewhere? I’m mostly interested in the
package.json
(specifically thetype
property) andtsconfig
if you have one (and this is Typescript). Also, is this a Typescript project or a regular Javascript project?Sorry, there’s several variables here that affect the proper solution.
EDIT : One more question. If this is a Typescript project, how are you executing it? Transpile then run with node? ts-node? ts-node-esm?
I am using ts-node
I don’t know how to help you directly, but when I get a strange error message I try to search for it, e.g. search for “Error [ERR_REQUIRE_ESM]: require() of ES Module”.
Or read the sentence that says “Instead change the require of index.js…” and do like it says. If you don’t know what a “dynamic import()” is (I also don’t know what it is), google for that.
from the error it looks like you’re importing an es module inside a common.js environment. but as @clif@lemmy.world said, there are several things that could cause this.
Update: updated some thing in my ts-config, I need to complie it down to javascript now but it works. I use tsc to compile it