Skip to content
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

Error when using with NestJS #113

Closed
mandrillxx opened this issue Dec 22, 2022 · 7 comments
Closed

Error when using with NestJS #113

mandrillxx opened this issue Dec 22, 2022 · 7 comments

Comments

@mandrillxx
Copy link

mandrillxx commented Dec 22, 2022

Firstly, I can't import using

import PocketBase from 'pocketbase'

because of this similar issue: #34 so I'm using

const PocketBase = require('pocketbase/cjs'); 

which doesn't provide types.

Now, I'm getting this error.

ERROR [WsExceptionsHandler] fetch is not defined
ReferenceError: fetch is not defined
    at t.call (C:\Users\Desktop\nest-app\node_modules\pocketbase\src\Client.ts:247:13)

I've installed node-fetch but this is still the error I get. Is there a solution?

@ganigeorgiev
Copy link
Member

I'm not sure how to reproduce the issue.

  • What version of node and nestjs are you using?
  • Do you have "type":"module" in your package.json?
  • Are you using ESM or CJS?

I've installed node-fetch but this is still the error I get. Is there a solution?

How have you installed node-fetch? You'll need to register the polyfill to the global context. I recommend https://github.com/lquixada/cross-fetch:

// npm install cross-fetch --save
require('cross-fetch/polyfill');

This and other older node version caveats you can find in https://github.com/pocketbase/js-sdk#nodejs-via-npm.

@mandrillxx
Copy link
Author

mandrillxx commented Dec 22, 2022

I'm not sure how to reproduce the issue.

  • What version of node and nestjs are you using?
  • Do you have "type":"module" in your package.json?
  • Are you using ESM or CJS?

I've installed node-fetch but this is still the error I get. Is there a solution?

How have you installed node-fetch? You'll need to register the polyfill to the global context. I recommend https://github.com/lquixada/cross-fetch:

// npm install cross-fetch --save
require('cross-fetch/polyfill');

This and other older node version caveats you can find in https://github.com/pocketbase/js-sdk#nodejs-via-npm.

this is exactly what I did:

  1. nest new & created project
  2. yarn add pocketbase
  3. in app.service.ts, i added the following
import { Injectable } from '@nestjs/common';
import PocketBase from 'pocketbase';

@Injectable()
export class PocketBaseService {
  public pb: PocketBase.default;

  constructor() {
    this.pb = new PocketBase.default('http://localhost:8090');
  }
}

however when i run this, i get this error in the console.

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\Desktop\nest-app-pocketbase\node_modules\pocketbase\dist\pocketbase.es.mjs not supported.
Instead change the require of C:\Users\Desktop\nest-app-pocketbase\node_modules\pocketbase\dist\pocketbase.es.mjs to a dynamic import() which is available in all CommonJS modules.

now, if i replace import PocketBase from 'pocketbase'; with const PocketBase = require('pocketbase/cjs'); , it will start, however not only do I not get type imports, any feature that requires using fetch gives me the error i mentioned,

ERROR [WsExceptionsHandler] fetch is not defined
ReferenceError: fetch is not defined
    at t.call (C:\Users\Desktop\nest-app\node_modules\pocketbase\src\Client.ts:247:13)

so require('cross-fetch/polyfill'); works, but theres still no type imports and its still bizarre having to use require twice.
when doing "type": "module" in package.json, theres a million other errors because of NestJS.

@ganigeorgiev
Copy link
Member

Ah, I now remember that there was a similar nest issue (#52).

Nestjs doesn't currently support out of the box ESM (or at least the last time I checked).
Please apply the suggested fixes in #52 (comment) (there are also links to several related nest issues that you could further explore).

@mandrillxx
Copy link
Author

Ah, I now remember that there was a similar nest issue (#52).

Nestjs doesn't currently support out of the box ESM (or at least the last time I checked). Please apply the suggested fixes in #52 (comment) (there are also links to several related nest issues that you could further explore).

I've done all those workarounds, but I'm still getting ReferenceError: fetch is not defined when using the sdk.
How hard would it be to convert this repo into a compatible module?

@ganigeorgiev
Copy link
Member

@mandrillxx How you are loading the polyfill? As mentioned previously you need to load it in the global context, aka. global.fetch = yourpolyfill. lquixada/cross-fetch would do that for your (there are also other things related to fetch that needs polyfill like the AbortController):

require('cross-fetch/polyfill');
// or 
import 'cross-fetch/polyfill';

You don't need a polyfill if you upgrade to node17+.

How hard would it be to convert this repo into a compatible module?

I don't understand what do you mean. The JS SDK is ES module. We have also a fallback for CJS. The types highly depends on your TS config and I'm not sure how to help you without any information on what node and ts versions you are using and what is your ts config.

@mandrillxx
Copy link
Author

@mandrillxx How you are loading the polyfill? As mentioned previously you need to load it in the global context, aka. global.fetch = yourpolyfill. lquixada/cross-fetch would do that for your (there are also other things related to fetch that needs polyfill like the AbortController):

require('cross-fetch/polyfill');
// or 
import 'cross-fetch/polyfill';

You don't need a polyfill if you upgrade to node17+.

How hard would it be to convert this repo into a compatible module?

I don't understand what do you mean. The JS SDK is ES module. We have also a fallback for CJS. The types highly depends on your TS config and I'm not sure how to help you without any information on what node and ts versions you are using and what is your ts config.

I'm going to use it without types and with require, it's too messy otherwise. What I mean though is that this module doesn't just work out of the box like many others, at least with nestjs.

@ganigeorgiev
Copy link
Member

@mandrillxx I'm not really sure how to explain it better.

First, you don't need a polyfill if you upgrade to Node 17+.

Second, this is not an issue with the SDK, but with Nest not supporting ESM out of the box (see nestjs/nest#10267 (comment), nestjs/nest#8736 and probably others). When you use import in nest you are not actually using ESM but CJS, aka. require (as mentioned in #52, you can explore the generated nest js files in your /dist folder and verify it).

If I apply locally the 3 changes suggested in #52 (comment), it works fine for me.

Another workaround is the suggestion in #52 (comment).

You've written that you've tried all of the above but there is not enough information to help you debug it. If you provide a minimal reproducible repo with what you've tried, I'll have a look at it, but by just saying "it doesn't work" I don't know how to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants