Closed newerton closed 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 :)
@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.
@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.
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)
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);
}
}
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