#`ERR_PACKAGE_PATH_NOT_EXPORTED` when starting my app

3 messages · Page 1 of 1 (latest)

lilac canyon
#

Hi,

I have been migrating from Mongoose to MikroORM with MongoDB the past two days. I've finally got rid of the type errors and fully refactored the code, but when I'm trying to run npm run start:dev or build and run after, I keep getting this error stack.

node:internal/modules/esm/resolve:313
  return new ERR_PACKAGE_PATH_NOT_EXPORTED(
         ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './bson' is not defined by "exports" in /api/node_modules/bson/package.json
    at exportsNotFound (node:internal/modules/esm/resolve:313:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:661:9)
    at resolveExports (node:internal/modules/cjs/loader:679:36)
    at Module._findPath (node:internal/modules/cjs/loader:746:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1406:27)
    at defaultResolveImpl (node:internal/modules/cjs/loader:1059:19)
    at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1064:22)
    at Module._load (node:internal/modules/cjs/loader:1227:37)
    at TracingChannel.traceSync (node:diagnostics_channel:328:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:245:24) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v24.11.1
$ npm ls bson
api@0.0.1 /api
└─┬ @mikro-orm/mongodb@6.6.2
  ├── bson@6.10.4
  └─┬ mongodb@6.20.0
    └── bson@6.10.4 deduped

I've tried downgrading/upgrading and overriding bson in my package.json without success.

MikroORM deps:

"@mikro-orm/core": "^6.6.2",
"@mikro-orm/mongodb": "^6.6.2",
"@mikro-orm/nestjs": "^6.1.1",

Appreciate if anybody has an idea on this.

#

This seems to occur when it's trying to connect to the the database.

This how I initialize MikroOrmModule

MikroOrmModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: async (config: ConfigService) => ({
    clientUrl: config.get<string>('MONGODB_URI'),
    dbName: config.get<string>('MONGODB_NAME'),
    autoLoadEntities: true,
    ensureIndexes: true,
    driver: MongoDriver,
  }),
}),
lilac canyon
#

It has been solved, but I will document what's happening here in case anybody ever needs help with it.
The MikroORM team helped me track the error down and it seems to be caused by @nestjs/swagger.

I had a BaseEntity that looked like this

import { Entity, PrimaryKey, ObjectId, SerializedPrimaryKey, Property, OptionalProps } from '@mikro-orm/mongodb';

@Entity({ abstract: true })
export abstract class BaseEntity<Optional = never> {
  [OptionalProps]?: 'createdAt' | 'updatedAt' | Optional;

  @PrimaryKey()
  _id!: ObjectId;

  @SerializedPrimaryKey()
  id!: string;

  @Property({ onCreate: () => new Date() })
  createdAt!: Date;

  @Property({ onCreate: () => new Date(), onUpdate: () => new Date() })
  updatedAt!: Date;
}

When the typescript code compiles, this base.entity.js is generated:

const openapi = require("@nestjs/swagger");
const mongodb_1 = require("@mikro-orm/mongodb");
let BaseEntity = class BaseEntity {
    [mongodb_1.OptionalProps];
    _id;
    id;
    createdAt;
    updatedAt;
    static _OPENAPI_METADATA_FACTORY() {
        return { _id: { required: true, type: () => require("bson/bson").ObjectId }, id: { required: true, type: () => String }, createdAt: { required: true, type: () => Date }, updatedAt: { required: true, type: () => Date } };
    }
};

The error occurs at require("bson/bson").ObjectId, and swagger complains.

This was solved by decorating the _id property in my BaseEntity with ApiHideProperty() (or you could also use ApiProperty({ type: String }))

This had been posted before, but my mind did not go to swagger at all before finding where the error occurred. https://github.com/nestjs/swagger/issues/1563

GitHub

I'm submitting a Bug report [ ] Regression [x] Bug report [ ] Feature request [ ] Documentation issue or request [ ] Support request => Please do not submit support request here, instead pos...