typescriptghost-blogesbuild

How to import an external module with TypeScript and esbuild?


I'm writing a storage adapter for Ghost using the @aws-sdk/client-s3. I want the adapter to be self-contained, so I'm trying to use esbuild to bundle it all into a single file. To build a storage adapter, you have to extend the BaseStorageAdapter class, which is available in Ghost, so I don't want to bundle that. However, esbuild outputs a different file depending on the syntax I use to import it, and I can't get it to generate the output I want.

The command I'm using is npx esbuild src/index.ts --external:ghost-storage-base --bundle --platform=node. Depending on the way I import ghost-storage-base, it generates the following output:

My tsconfig.json is currently quite basic:

{
    "include": [
        "src/**/*"
    ],
}

Am I calling esbuild wrong? Is there a TypeScript configuration option I'm missing? Or is it something else?


Solution

  • The problem was that I forgot to export the class I declared. I just added

    module.exports = MyClass
    

    to the bottom of the file, and it worked perfectly.

    export MyClass
    

    also worked.