Trying to make my plugin work as npm package with bun

Hello everyone, I built a better-auth plugin. It works perfectly when I use it directly in my app. But it doesn't work when I try to put it as an npm package. I tried to bun link it with
bun link my-plugin
bun link my-plugin
and copied the way @better-auth/stripe handles it. But it still gives me this error: Module not found: Can't resolve 'my-plugin' 1 | import { betterAuth} from "better-auth";
2 | import { myPlugin } from "my-plugin";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Module not found: Can't resolve 'my-plugin/client' 1 | import { createAuthClient } from "better-auth/react";
2 | import { myPluginClient} from "my-plugin/client";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8 Replies
Maqed
MaqedOP2w ago
Here is my configuration: package.json:
{
"name": "my-plugin",
"version": "0.0.1",
"module": "dist/index.mjs",
"main": "dist/index.cjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "vitest",
"build": "unbuild",
"dev": "unbuild --watch",
"prepublishOnly": "npm run build",
"release": "release-it"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./client": {
"types": "./dist/client.d.ts",
"import": "./dist/client.mjs",
"require": "./dist/client.cjs"
}
},
"typesVersions": {
"*": {
"*": [
"./dist/index.d.ts"
],
"client": [
"./dist/client.d.ts"
]
}
},
"peerDependencies": {
"better-auth": "^1.2.5"
},
"devDependencies": {
"@types/bun": "^1.2.8",
"bun-plugin-dts": "^0.3.0",
"release-it": "^19.0.1",
"typescript": "^5.0.0"
},
"dependencies": {
"unbuild": "^3.5.0",
"zod": "^3.24.2"
},
"publishConfig": {
"access": "public"
}
}
{
"name": "my-plugin",
"version": "0.0.1",
"module": "dist/index.mjs",
"main": "dist/index.cjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "vitest",
"build": "unbuild",
"dev": "unbuild --watch",
"prepublishOnly": "npm run build",
"release": "release-it"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./client": {
"types": "./dist/client.d.ts",
"import": "./dist/client.mjs",
"require": "./dist/client.cjs"
}
},
"typesVersions": {
"*": {
"*": [
"./dist/index.d.ts"
],
"client": [
"./dist/client.d.ts"
]
}
},
"peerDependencies": {
"better-auth": "^1.2.5"
},
"devDependencies": {
"@types/bun": "^1.2.8",
"bun-plugin-dts": "^0.3.0",
"release-it": "^19.0.1",
"typescript": "^5.0.0"
},
"dependencies": {
"unbuild": "^3.5.0",
"zod": "^3.24.2"
},
"publishConfig": {
"access": "public"
}
}
tsconfig.json
{
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"module": "ESNext",
"noEmit": true,
"moduleResolution": "Bundler",
"moduleDetection": "force",
"isolatedModules": true,
"verbatimModuleSyntax": true,
"strict": true,
"noImplicitOverride": true,
"noFallthroughCasesInSwitch": true
},
"exclude": ["node_modules", "dist"],
"include": ["src"]
}
{
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"module": "ESNext",
"noEmit": true,
"moduleResolution": "Bundler",
"moduleDetection": "force",
"isolatedModules": true,
"verbatimModuleSyntax": true,
"strict": true,
"noImplicitOverride": true,
"noFallthroughCasesInSwitch": true
},
"exclude": ["node_modules", "dist"],
"include": ["src"]
}
build.config.ts
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
declaration: true,
rollup: {
emitCJS: true,
inlineDependencies: true,
},
entries: ["./src/index", "./src/client"],
outDir: "dist",
clean: true,
failOnWarn: false,
});
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
declaration: true,
rollup: {
emitCJS: true,
inlineDependencies: true,
},
entries: ["./src/index", "./src/client"],
outDir: "dist",
clean: true,
failOnWarn: false,
});
My folder structure ./root --/src ---/my-plugin.ts ---/client.ts ---/index.ts ---/other-files.... I also tried to release it as an actual npm package not just a link but still not working
Shyam Verma
Shyam Verma2w ago
what doest your plugin do?
Maqed
MaqedOP2w ago
extend organization plugin with more functionality
Shyam Verma
Shyam Verma2w ago
It might be due to wrong module names, missing packages, or Bun not resolving modules properly.
Maqed
MaqedOP2w ago
I'll try using npm and pnpm and see what happens.. It's still not working....
Maqed
MaqedOP2w ago
It somtimes gives this error
No description
Maqed
MaqedOP2w ago
Other times it gives this (This only shows in IDE)
No description
Maqed
MaqedOP2w ago
When I try to run
bun dev
bun dev
It always gives me this error: Cannot find module 'extended-organization/client' or its corresponding type declarations.ts(2307) Probably the problem from link: functionality in my device. I published the package to npm & everything is working perfectly. Note: I had to add .npmignore file & exclude everything that is in .gitignore EXCEPT dist folder

Did you find this page helpful?