1 year ago

#378675

test-img

Joe Lapp

Defining a tsconfig for CLI tools within a svelte-kit package

I've written a svelte-kit app that hits a backend database.

I've also written CLI tools needed to maintain the database alongside the app.

I want to keep the app and the CLI tools in the same repo, because they share code, but the "type":"module" that svelte-kit requires in package.json prevents me from compiling the CLI tools using the svelte-kit tsconfig.

So I set out to create a tsconfig.tools.json for building the CLI tools. But I have not yet succeeded in crafting one that works despite having "type":"module" in package.json

Here's my latest attempt:

{
    "compilerOptions": {
        "module": "CommonJS",
        "target": "es2020",
        "lib": ["es2020"],
        "moduleResolution": "node",
        "strict": true,
        "pretty": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "noEmitOnError": true,
        "outDir": "build"
    },
    "include": [
        "src/tools/**/*.ts",
        "src/model/**/*.ts",
        "src/shared/**/*.ts",
        "src/integrations/**/*.ts",
    ],
    "exclude": [
        "node_modules/**",
        "./**/*.test.ts"
    ]
}

This attempt yields the following error on compilation:

ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and
'/Users/joe/repos/cavesite/package.json' contains "type": "module". To treat it as a
CommonJS script, rename it to use the '.cjs' file extension.

TypeScript has been very creative with the reasons it gives for not building my CLI tool, depending on what I put in tsconfig.tools.json. The above is just the latest reason.

Any suggestions on how I accomplish this? I'd hate to have to factor out the common code into a separate repo.

typescript

tsconfig

sveltekit

0 Answers

Your Answer

Accepted video resources