codenotary / immudb-node

Node.js SDK for immudb
Apache License 2.0
62 stars 2 forks source link

Error Require ES Module #35

Closed newerton closed 1 year ago

newerton commented 1 year ago

Error [ERR_REQUIRE_ESM]: require() of ES Module \www\skeleton-api\node_modules\@codenotary\immudb-node\dist\index.js from \www\skeleton-api\dist\src\app\app.service.js not supported. Instead change the require of index.js in \www\skeleton-api\dist\src\app\app.service.js to a dynamic import() which is available in all CommonJS modules.

nest -v 9.1.5 node -v v16.15.0

import { Client } from '@codenotary/immudb-node';
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  async createTable(): Promise<void> {
    const client = new Client({
      host: '127.0.0.1',
      port: 3322,
      user: 'immudb',
      password: 'immudb',
      database: 'defaultdb',
    });
    const {
      subTxes: [{ tx: createTestTableTx }],
    } = await client.sqlExec({
      sql: `
        create table if not exists testtable (
            id1         integer not null,
            id2         varchar[3] null,
            created     timestamp null,
            data        varchar[512] not null,
            isActive    boolean not null,
            primary key (id1, id2)
        );
    `,
    });
    console.log('createTestTableTx:', createTestTableTx);
  }
}
tomekkolo commented 1 year ago

Hi @newerton ,

this issue can be solved with adding "type":"module" to your package.json file. Also, we just fix issue with imports (https://github.com/codenotary/immudb-node/pull/36) and published it to npm.

Hope this helps. Let us know :)

newerton commented 1 year ago

@tomekkolo Thanks for feedback!

I already made that change. But the typescript build of nestjs is commonjs, so it gives an error because the immudb-node build is esm.

I made an internal correction, copied the code from immudb-node, immudb-node-grpcjs and immudb-node-pbjs. Now the build is done in commonjs, and it worked.

In version 2, you could remove the type: module from the packages, and let package.json or tsconfig.json define the build.

tomekkolo commented 1 year ago

@newerton , you can use dynamic import to use it with nestjs. ESM is a way to go for us and many other packages, especially as the package can be still used in commonjs.

newerton commented 1 year ago

I had already used dynamic import and it also does not work.

import { Injectable } from '@nestjs/common';

let Client = null;

@Injectable()
export class AppService {
  async getHello(): Promise<string> {
    Client = await import('@codenotary/immudb-node');

    const defaultClient = new Client({
      host: '127.0.0.1',
      port: 3322,
      user: 'immudb',
      password: 'immudb',
      database: 'defaultdb',
    });

    console.log('Log something');

    await defaultClient.close();

    return 'Hello World!';
  }
}
curl localhost:3000

Error [ERR_REQUIRE_ESM]: require() of ES Module \www\newerton\project-name\node_modules\@codenotary\immudb-node\dist\index.js from \www\newerton\project-name\dist\app.service.js not supported. Instead change the require of index.js in \www\newerton\project-name\dist\app.service.js to a dynamic import() which is available in all CommonJS modules. at \www\newerton\project-name\dist\app.service.js:14:53 at async AppService.getHello (\www\newerton\project-name\dist\app.service.js:14:18)

tomekkolo commented 1 year ago

Hey @newerton,

try to do it that way:

import { Injectable } from '@nestjs/common';

const dynamicImport = async (packageName: string) => new Function(`return import('${packageName}')`)();

@Injectable()
export class AppService {
    async createTable(): Promise<void> {
        const client = new (await (dynamicImport('@codenotary/immudb-node'))).Client({
          host: '127.0.0.1',
          port: 3322,
          user: 'immudb',
          password: 'immudb',
          database: 'defaultdb',
        });
        const {
          subTxes: [{ tx: createTestTableTx }],
        } = await client.sqlExec({
          sql: `
            create table if not exists testtable (
                id1         integer not null,
                id2         varchar[3] null,
                created     timestamp null,
                data        varchar[512] not null,
                isActive    boolean not null,
                primary key (id1, id2)
            );
        `,
        });
        console.log('createTestTableTx:', createTestTableTx);
      }
}