-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@libsql/client
can't be used from CommonJS modules using TypeScript
#182
Labels
bug
Something isn't working
good first issue
Good for newcomers
help wanted
Extra attention is needed
Comments
If you still want the package.json snippet"exports": {
".": {
"import": {
"types": "./lib-esm/node.d.ts",
"workerd": "./lib-esm/web.js",
"deno": "./lib-esm/web.js",
"edge-light": "./lib-esm/web.js",
"netlify": "./lib-esm/web.js",
"node": "./lib-esm/node.js",
"browser": "./lib-esm/web.js",
"default": "./lib-esm/node.js"
},
"require": {
"types": "./lib-cjs/node.d.ts",
"default": "./lib-cjs/node.js"
}
},
"./node": {
"import": {
"types": "./lib-esm/node.d.ts",
"default": "./lib-esm/node.js"
},
"require": {
"types": "./lib-cjs/node.d.ts",
"default": "./lib-cjs/node.js"
}
},
"./http": {
"import": {
"types": "./lib-esm/http.d.ts",
"default": "./lib-esm/http.js"
},
"require": {
"types": "./lib-cjs/http.d.ts",
"default": "./lib-cjs/http.js"
}
},
"./ws": {
"import": {
"types": "./lib-esm/ws.d.ts",
"default": "./lib-esm/ws.js"
},
"require": {
"types": "./lib-cjs/ws.d.ts",
"default": "./lib-cjs/ws.js"
}
},
"./sqlite3": {
"import": {
"types": "./lib-esm/sqlite3.d.ts",
"default": "./lib-esm/sqlite3.js"
},
"require": {
"types": "./lib-cjs/sqlite3.d.ts",
"default": "./lib-cjs/sqlite3.js"
}
},
"./web": {
"import": {
"types": "./lib-esm/web.d.ts",
"default": "./lib-esm/web.js"
},
"require": {
"types": "./lib-cjs/web.d.ts",
"default": "./lib-cjs/web.js"
}
}
}, tsc --traceResolution
|
penberg
added
bug
Something isn't working
good first issue
Good for newcomers
help wanted
Extra attention is needed
labels
Feb 27, 2024
Hello I had the same issue , what did the trick for me was setting those in my
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
Something isn't working
good first issue
Good for newcomers
help wanted
Extra attention is needed
@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
with a fairly basic tsconfig:
tsconfig.json
and then import
@libsql/client
insrc/index.ts
src/index.ts
You will get the following error:
This appears to be caused by
@libsql/client
sharing the.d.ts
files across both thelib-esm
andlib-cjs
folders.If you ask the TypeScript compiler to explain its module resolution (
tsc --traceResolution
), you get this:tsc --traceResolution
We can see the module resolution is using
exports/./types
to find thelib-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 thelib-cjs
folder in the package (so that they are duplicated), and remove all thetypes
properties underexports
(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
While this does seem like dodgy TypeScript compiler behaviour, I'd suggest just copying the .d.ts files into both
lib-esm
andlib-cjs
and removing thetypes
properties from theexports
entries to allow the compiler to find them dynamically. That appears to solve the problem.The text was updated successfully, but these errors were encountered: