-
Notifications
You must be signed in to change notification settings - Fork 128
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
This does not work with nest js #52
Comments
I'll test it later today with nest, but most likely this a transpiler (eg. TypeScript) misconfiguration, so make sure that your nest project can load ES modules. The easiest workaround for your error would be to load the CommonJS PocketBase bundle like: const PocketBase = require('pocketbase/cjs'); The above should work because it is a native CommonJS module, where the default |
This kind of connection will work, but will block eslint. It doesn't look uniform. I tried to fix it, but my skills are lacking. |
@DenisVASI9 i'm not sure that I understand what do you mean by " but will block eslint". PocketBase is ESM by default. The legacy CJS bundle is If you want to use CommonJS, use The error indicates that you are using Currently the nestjs template doesn't seem to come with ESM support by default (see nestjs/nest#10267 (comment), nestjs/nest#8736, etc.). If you want to use ES modules without hacky interops, you'll have to:
In any case, this is not related to PocketBase and you'll stumble on the same error if you try to import any other ESM package. |
Thank you! |
@ganigeorgiev |
@DenisVASI9 What option did you end up using? Once again, this is not related to the SDK and I'm not sure how to help without any information on your setup (eg. My advice would be to apply the listed 3 steps from above and start using ESM and import the SDK just by: import PocketBase from "pocketbase"; If you want to allow loading other CJS modules with ESM you can also add |
@ganigeorgiev I've been using import PocketBase from "pocketbase/cjs" I was hoping to use cjs without changing the nest tranpiler settings. |
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "Node",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"lib": ["ESNext"],
"target": "ESNext",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"strict": true,
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": true,
"noImplicitAny": true,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
|
@DenisVASI9 You can use Update: You may also need to add {
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "nodenext", <--- changed
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true, <--- added
"lib": ["ESNext"],
"target": "ESNext",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"strict": true,
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": true,
"noImplicitAny": true,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
} But once again, my recommendation would be to migrate to es modules entirely since you'll stumble on similar errors down the road with other packages that may not have cjs alternative. Or just use |
Hi, in case someone is stuck with this issue - I was able to do an unorthodox fix for this issue using Compile a package that depends on ESM only library into a CommonJS package This is my code for a NestJS Service class with pocketbase instance as a property: import { Injectable, OnModuleDestroy, OnModuleInit } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Cron, CronExpression } from "@nestjs/schedule";
import type Pocketbase from "pocketbase";
@Injectable()
export class PocketbaseService implements OnModuleInit, OnModuleDestroy {
private _client: Pocketbase;
constructor(
private readonly config: ConfigService
) {}
get client() {
return this._client;
}
async onModuleInit() {
// create client
const pocketbase = await (eval(`import('pocketbase')`) as Promise<typeof import('pocketbase')>);
const PocketbaseClass = pocketbase.default;
this._client = new PocketbaseClass(this.config.get('POCKETBASE_URL'));
// login as admin
await this.client.admins.authWithPassword(this.config.get('POCKETBASE_ADMIN_EMAIL'), this.config.get('POCKETBASE_ADMIN_PASSWORD'));
}
async onModuleDestroy() {
// logout
this.client.authStore.clear();
}
@Cron(CronExpression.EVERY_5_MINUTES)
async reAuth() {
// re-authenticate every 5 minutes
await this.client.admins.authRefresh();
}
} |
Worked perfectly. Thanks. |
This is gold! Worked so perfectly. Thank you. |
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/hermione/cd-market-backend/node_modules/pocketbase/dist/pocketbase.es.mjs not supported.
Instead change the require of /home/hermione/cd-market-backend/node_modules/pocketbase/dist/pocketbase.es.mjs to a dynamic import() which is available in all CommonJS modules.
The text was updated successfully, but these errors were encountered: