Error when importing a local package made with Bun

105 Views Asked by At

I've created a package with Bun and mongoose in TS, to import that package I've followed the suggested configuration from "Crafting a Typescript NPM package with Bun - Function Agents", finally I run these commands:

$ bun run build
$ bun link
bun link v1.0.25 (a8ff7be6)
Success! Registered "@warcayac/utils"

To use @warcayac/utils in a project, run:
  bun link @warcayac/utils

Or add it in dependencies in your package.json file:
  "@warcayac/utils": "link:@warcayac/utils"

My package.json is:

{
  "name": "@warcayac/utils",
  "module": "index.ts",
  "type": "module",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "version": "0.0.1",
  "description": "A collection of utilities for backend and frontend projects",
  "scripts": {
    "build": "bun build --target=node ./src/index.ts --outfile=dist/index.js && bun run build:declaration",
    "build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json",
    "postbuild": "rimraf tsconfig.types.tsbuildinfo"
  },
  "devDependencies": {
    "bun-types": "latest",
    "rimraf": "^5.0.5"
  },
  "peerDependencies": {
    "typescript": "^5.3.3"
  },
  "dependencies": {
    "mongoose": "^8.1.0"
  },
  "keywords": [
    "utils"
  ],
  "author": "Me",
  "license": "MIT",
  "files": [
    "dist/*.js",
    "dist/*.d.ts"
  ]
}

and tsconfig.json

{
  "compilerOptions": {
    "lib": ["ESNext"],
    "target": "ESNext",
    "moduleDetection": "force",
    "jsx": "react-jsx",
    "module": "ESNext",
    "rootDir": "./src",
    "baseUrl": "./",
    "paths": { 
      "@/*": ["src/*"]
    },
    "types": [
      "bun-types"
    ],
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "noEmit": true,
    "outDir": "./dist",
    "downlevelIteration": true,
    "allowJs": true,
    "composite": true,
    "verbatimModuleSyntax": true,
    "forceConsistentCasingInFileNames": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "noFallthroughCasesInSwitch": true,
    "skipLibCheck": true
  },
  "include": [
    "src/**/*.ts"
  ]
}

After that I created an Elysia server project and added my package:

$ bun link --save @warcayac/utils

and my package was added successfully. I created a test.js to test it:

import MongoClient from "@warcayac/utils/dist/mongodb/MongoClient";
import { TNote, notesSchema } from "./schema";


if (process.argv.length < 3) {
  console.log('give password as argument')
  process.exit(1)
}

const password = process.argv[2];

async function main() {
  const client = new MongoClient<TNote>(
    `mongodb+srv://aaaa:${password}@server.net/notesDb?retryWrites=true&w=majority`,
    notesSchema,
    true,
  );

  await client.connect();
  const data = await client.getCollection();
  console.log(data);
  await client.disconnect();
}

main();

this line was added by editor when recognizing MongoClient class,

import MongoClient from "@warcayac/utils/dist/mongodb/MongoClient";

and it is recognized with all its properties and methods, but when running this file:

$ bun run ./src/services/mongodb/test.ts "123456789"

I get this error message:

error: Cannot find module "@warcayac/utils/MongoClient" from "./notes_server/src/services/mongodb/test.ts"

Any idea about how to fix this error?

this is my package.json for my Elysia project:

{
  "name": "notes_server",
  "version": "1.0.50",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "bun run --watch src/index.ts",
    "start": "bun run src/index.ts"
  },
  "dependencies": {
    "@elysiajs/cors": "^0.8.0",
    "@elysiajs/static": "^0.8.1",
    "@warcayac/utils": "link:@warcayac/utils",
    "ansis": "^2.0.3",
    "elysia": "latest",
    "mongoose": "^8.1.0"
  },
  "devDependencies": {
    "bun-types": "latest"
  },
  "module": "src/index.js"
}

and tsconfig.json

{
  "compilerOptions": {
    "target": "ES2021",
    "module": "ES2022",
    "moduleResolution": "node",
    "types": ["bun-types"],
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}
0

There are 0 best solutions below