Just a geek, finding my way in the fediverse.

  • 0 Posts
  • 11 Comments
Joined 1 year ago
cake
Cake day: June 11th, 2023

help-circle





  • I had to check and make sure I didn’t type the comment above because it sounds exactly like me.

    All UIs do things slightly differently, the CLI is always exactly the same… Everywhere. UI for non trivial conflict resolution? Definitely. For everything else, CLI.

    And, I’m also reticent to use rebase unless I have to. Gimme that good ole FF :)



  • 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.



  • 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.


  • This is a great answer. Bumping it as someone who got forced to move into Node/JS around 8 years ago and came to love it (after the ES2015 changes :). It’s primarily what I work in and I teach community classes on it these days.

    I’ve been dabbling in Go lately for lower level server side stuff and, while I don’t dislike it, it’s a big shift in thinking. There are a lot of niceties to the Go ecosystem.