Skip to content

import Package is not available in all CommonJS modules #196

@Trandx

Description

@Trandx

What's the good way to use this package in Nestjs ?

I have tried to make test, but nestjs Builder has Throw this error

Error [ERR_REQUIRE_ESM]: require() of ES Module docker_test/app/node_modules/.pnpm/@authzed+authzed-node@1.2.0/node_modules/@authzed/authzed-node/dist/src/index.js from /docker_test/app/dist/app.service.js not supported.
Instead change the require of index.js in /dev/docker_test/app/dist/app.service.js to a dynamic import() which is available in all CommonJS modules.

this is the code of app.service.ts

`
import { v1 } from '@authzed/authzed-node';
import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class AppService {

private readonly logger = new Logger(AppService.name);
private client: v1.ZedClientInterface;

constructor() {
  // Initialize AuthZed (SpiceDB) gRPC Client
  this.client = v1.NewClient(
    process.env.SPICEDB_TOKEN || 'mysecret', // Use ConfigService for env vars
    process.env.SPICEDB_HOST || 'localhost:50051', // Use ConfigService for env vars
  );
}

async checkPermission(): Promise<any> {
  try {
    // Create the resource reference
    const firstPost = v1.ObjectReference.create({
      objectType: 'blog/post',
      objectId: '1',
    });

    // Create the user reference
    const emilia = v1.ObjectReference.create({
      objectType: 'blog/user',
      objectId: 'emilia',
    });

    // Create the subject reference using the user reference
    const subject = v1.SubjectReference.create({
      object: emilia,
    });

    // Create the permission check request
    const checkPermissionRequest = v1.CheckPermissionRequest.create({
      resource: firstPost,
      permission: 'read',
      subject,
    });

    // Use await to handle the asynchronous call
    const response = this.client.checkPermission(checkPermissionRequest, ((err, resp) => {
      console.error(err)
      console.log(resp)

      return resp || err
    }));
    
   this.logger.log('Permission check response:', response);

    return response;
  } catch (error) {
    this.logger.error('Error checking permission:', error);
    throw error; // Re-throw the error to be handled by the caller
  }
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions