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’;

  • clif@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    1 year ago

    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 the type property) and tsconfig 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?

    EDIT x2 : If you’ll throw the project, or a simplified example that demonstrates the problem, up on github or somewhere else publicly accessible we should be able to fix you up pretty quickly. You can exclude the node_modules/ folder.

    • skymtfOP
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      __{ "name": "hourlypets", "version": "1.0.0", "description": "A bot for displaying hourly pets", "main": "dist/index.js", "scripts": { "start": "node dist/index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@types/node": "^20.5.1", "typescript": "^5.1.6" }, "dependencies": { "@petfinder/petfinder-js": "^1.0.6", "dotenv": "^16.3.1", "tsl-mastodon-api": "^0.4.0" } } _

      { "compilerOptions": { "module": "es6", "esModuleInterop": true, "target": "es6", "moduleResolution": "node", "sourceMap": true, "outDir": "dist" }, "lib": ["es2015"] }

      • clif@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        Hehe, this is the same type of answer I get from clients when I ask more than one question at a time 😆

        Do you have answers to the other questions? Or, much better, can you post the code including your package.json and tsconfig to github/etc?

          • clif@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            edit-2
            1 year ago

            Try simply adding the following to your package.json: "type": "module"

            I tested this with import * as Mastodon from 'tsl-mastodon-api'; in a test.js file using the example from their readme, executing with ts-node test.js (NOTE THAT THIS IS A JS FILE! Not a .ts file) and it works. It times out, of course, because the example URL doesn’t exist, but you get the idea. Minimal example (package.json is default from npm init -y but with the "type": "module" added. I also tested with your package.json [adding type:module] and tsconfig.json):

            test.js :

            import * as Mastodon from 'tsl-mastodon-api';
            
            const mastodon = new Mastodon.API({
                access_token: 'ABC',
                api_url: 'https://mastodon.example/api/v1/'
            });
            
            const result = await mastodon.postStatus({
                sensitive: true,
                spoiler_text: 'Hello',
                status: 'World'
            });
            console.log(JSON.stringify(result));
            

            Full package.json that you provided with the “type” property added :

              {
                "name": "hourlypets",
                "type": "module",
                "version": "1.0.0",
                "description": "A bot for displaying hourly pets",
                "main": "dist/index.js", 
                "scripts": {
                  "start": "node dist/index.js", 
                  "test": "echo \"Error: no test specified\" && exit 1"
                },
                "keywords": [],
                "author": "",
                "license": "ISC",
                "devDependencies": {
                  "@types/node": "^20.5.1",
                  "typescript": "^5.1.6"
                },
                "dependencies": {
                  "@petfinder/petfinder-js": "^1.0.6",
                  "dotenv": "^16.3.1",
                  "tsl-mastodon-api": "^0.4.0"
                }
              }
            

            On the note that I used test.js - if this was instead test.ts and you tried to execute it directly with ts-node like ts-node test.ts it will throw the error TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for test.ts. So, I made the assumption that your file is a JS based solely on that fact.

            If you still have problems, post a super simplified script like my test.js above that causes the issue for you and I’ll test with yours. You can exclude all private code - just enough to trigger the module error… meaning you could probably just have the import and console.log('hi'); and that would be sufficient.

            • skymtfOP
              link
              fedilink
              arrow-up
              1
              ·
              1 year ago

              so attempting this causes all typescript stuff to break, sadly some of my code depends on typescript specific things.

            • skymtfOP
              link
              fedilink
              arrow-up
              1
              ·
              1 year ago

              I am using TS for pretty much the entire project, but I know TS and JS are pretty much the same with a few added rules.

        • skymtfOP
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          just posted it here, the code is currently not public but I posted the ts-config and package.json

          • skymtfOP
            link
            fedilink
            arrow-up
            1
            ·
            1 year ago

            At one point I did have if set to commonJS too, but it seemed to cause issues. Sending the one the is updated as a suggestion

  • Empathy [he/him]@beehaw.org
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    My first impression is that it says you’re trying to load an ES Module using a CommonJS require() call. Evidently from your code snippet, that’s not the case. My guess would be that Typescript is transpiling your code to CommonJS modules, and that the library you’re trying to import only exports ES modules (or at-least that’s what NodeJS is trying to import for one reason or another).

    You may have to modify something in your tsconfig.json file to tell it to transpile to ESM instead of CommonJS.

  • skymtfOP
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    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

  • elmicha@feddit.de
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    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.

  • telepresence@discuss.tchncs.de
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    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.

    • skymtfOP
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      That’s exactly what I have been dealing with I believe.