tursodatabase / tursodatabase/libsql-client-ts

`@libsql/client` can't be used from CommonJS modules using TypeScript

Open
#182 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug good first issue help wanted
Dominant language
TypeScript
Stars
576
Forks
69
PR merge metrics
No merged PRs in 30d

Description

@libsql/client appears intended to be able to support both ESM and CJS modules, but unfortunately, it doesn't appear to work properly with the TypeScript compiler.

If one creates a simple NodeJS (v18) package like so:

package.json
{
  "name": "nodejs-test",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "author": "",
  "license": "ISC",
  "scripts": {
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@libsql/client": "^0.5.2"
  },
  "devDependencies": {
    "@tsconfig/node18": "^18.2.2",
    "typescript": "^5.3.3"
  }
}

with a fairly basic tsconfig:

tsconfig.json
{
  "extends": "@tsconfig/node18",
  "compilerOptions": {
    "outDir": "dist",
    "resolveJsonModule": true,
    "noUncheckedIndexedAccess": true,
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,
    "stripInternal": true,
  },
  "include": [
    "src/**/*",
    "test/**/*"
  ]
}

and then import @libsql/client in src/index.ts

src/index.ts
import {} from "@libsql/client";

You will get the following error:

src/index.ts:1:16 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@libsql/client")' call instead.
To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field "type": "module" to '/home/daniel/temp/nodejs-test/package.json'.

This appears to be caused by @libsql/client sharing the .d.ts files across both the lib-esm and lib-cjs folders.

If you ask the TypeScript compiler to explain its module resolution (tsc --traceResolution), you get this:

tsc --traceResolution
======== Resolving module '@libsql/client' from '/home/daniel/temp/nodejs-test/src/index.ts'. ========
Explicitly specified module resolution kind: 'Node16'.
Resolving in CJS mode with conditions 'require', 'types', 'node'.  <-------------------------------------------- Good
File '/home/daniel/temp/nodejs-test/src/package.json' does not exist according to earlier cached lookups.
File '/home/daniel/temp/nodejs-test/package.json' exists according to earlier cached lookups.
Loading module '@libsql/client' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/home/daniel/temp/nodejs-test/src/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'libsql__client'
Found 'package.json' at '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/package.json'.
Entering conditional exports.
Matched 'exports' condition 'types'. <-------------------------------------------------------- Hrm...
Using 'exports' subpath '.' with target './lib-esm/node.d.ts'.
File '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-esm/node.d.ts' exists - use it as a name resolution result.
Resolved under condition 'types'.
Exiting conditional exports.
Resolving real path for '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-esm/node.d.ts', result '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-esm/node.d.ts'.
======== Module name '@libsql/client' was successfully resolved to '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-esm/node.d.ts' with Package ID '@libsql/client/lib-esm/node.d.ts@0.5.2'. ========
File '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-esm/package.json' does not exist.
File '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/package.json' exists according to earlier cached lookups.
======== Resolving module '@libsql/core/api' from '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-esm/node.d.ts'. ========
Explicitly specified module resolution kind: 'Node16'.
Resolving in ESM mode with conditions 'import', 'types', 'node'. <--------------------------------------------- Bad

We can see the module resolution is using exports/./types to find the lib-esm/node.d.ts, which appears to be flipping the compiler into ESM mode (maybe it can see the ESM .js files next to the .d.ts files).

However, if we copy all the .d.ts files into the lib-cjs folder in the package (so that they are duplicated), and remove all the types properties under exports (so the TS compiler will search for .d.ts files next to the .js files instead), we get the correct behaviour and the error goes away. Here's what the module resolution trace output shows for that:

tsc --traceResolution
======== Resolving module '@libsql/client' from '/home/daniel/temp/nodejs-test/src/index.ts'. ========
Explicitly specified module resolution kind: 'Node16'.
Resolving in CJS mode with conditions 'require', 'types', 'node'. <--------------------------------------------- Good
File '/home/daniel/temp/nodejs-test/src/package.json' does not exist according to earlier cached lookups.
File '/home/daniel/temp/nodejs-test/package.json' exists according to earlier cached lookups.
Loading module '@libsql/client' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/home/daniel/temp/nodejs-test/src/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'libsql__client'
Found 'package.json' at '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/package.json'.
Entering conditional exports.
Saw non-matching condition 'import'.
Matched 'exports' condition 'require'. <--------------------------------------------------- Excellent
Using 'exports' subpath '.' with target './lib-cjs/node.js'.
File name '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.js' has a '.js' extension - stripping it.
File '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.ts' does not exist.
File '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.tsx' does not exist.
File '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.d.ts' exists - use it as a name resolution result. <---------------------- Yep
Resolved under condition 'require'.
Exiting conditional exports.
Resolving real path for '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.d.ts', result '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.d.ts'.
======== Module name '@libsql/client' was successfully resolved to '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.d.ts' with Package ID '@libsql/client/lib-cjs/node.d.ts@0.5.2'. ========
Found 'package.json' at '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/package.json'.
======== Resolving module '@libsql/core/api' from '/home/daniel/temp/nodejs-test/node_modules/@libsql/client/lib-cjs/node.d.ts'. ========
Explicitly specified module resolution kind: 'Node16'.
Resolving in CJS mode with conditions 'require', 'types', 'node'. <---------------------------------------- Seems good!

While this does seem like dodgy TypeScript compiler behaviour, I'd suggest just copying the .d.ts files into both lib-esm and lib-cjs and removing the types properties from the exports entries to allow the compiler to find them dynamically. That appears to solve the problem.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect package.json exports and the generated lib-esm and lib-cjs declaration files; reproduce the Node 18 TypeScript setup and run tsc --traceResolution. Done means a CommonJS TypeScript import resolves through the require path without TS1479 while ESM resolution remains working.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
build-system, developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.