inducingchaos / sendblue-ts

A Typescript Node.js wrapper for the Sendblue API, a service for seamlessly integrating your apps with iMessage.
https://sendblue.co
ISC License
1 stars 0 forks source link

Doesn't support importing into CommonJS file #1

Open coderpr0grammer opened 1 month ago

coderpr0grammer commented 1 month ago

When trying to import in my typescript file that gets compiled to commonjs:

import Sendblue from "@rileybarabash/sendblue-ts";

I get the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module @rileybarabash/sendblue-ts/dist/index.js from /functions/lib/index.js not supported.

I have also tried dynamic import:

import("@rileybarabash/sendblue-ts").then(({ default: Sendblue }) => {
        const sendblue = new Sendblue(process.env.SENDBLUE_PK ?? "", process.env.SENDBLUE_SK ?? "");
        sendblue.sendMessage({
            number: phoneNumber,
            content: message,
            statusCallback: "https://example.com/callback"
        }).then((response) => {
            console.log(response);
        }).catch((error) => {
            console.error(error);
        });
    });

But I get the same error.

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "esModuleInterop": true,
    "lib": ["es2021", "dom"],
    "allowSyntheticDefaultImports": true
  },
  "compileOnSave": true,
  "include": [
    "src",
    "ical.d.ts"
  ],
}
inducingchaos commented 1 month ago

Hey, unfortunately I wrote this wrapper with ESM-only projects in mind, since IMO it's a much cleaner syntax and a more modern way of doing things.

If you want to change your project to use ES modules, you can configure your tsconfig.json something like this:

/* @see [TSConfig Documentation](https://www.typescriptlang.org/tsconfig) */

{
    "compilerOptions": {
        /* Type checking. */

        "strict": true,

        /* Modules. */

        "module": "ESNext",
        "moduleResolution": "Bundler",

        /* Interop constraints. */

        "esModuleInterop": true,
        "isolatedModules": true,
        "verbatimModuleSyntax": true,

        /* Language and environment. */

        "lib": ["dom", "dom.iterable", "ESNext"],
        "moduleDetection": "force",
        "target": "ESNext"
    }
}

This will likely break a lot of things. If you don't want to change your bundler setup, you can just copy/paste the source code into your project to use. It's pretty simple, just a wrapper around the Sendblue API. I don't use or maintain this at the moment so that's about as much help as I can give.

Best of luck.