pglite icon indicating copy to clipboard operation
pglite copied to clipboard

Error loading uuid_ossp extensions

Open sergio-milu opened this issue 1 year ago • 5 comments

Hey,

Version: @electric-sql/pglite": "^0.2.14

I'm trying to setup PGLite in my typeorm project for testing. I'm using jest + SWC. The error that I'm getting is

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { d as PGliteInterface } from '../pglite-CCwqaSmq.js';
                                                                                      ^^^^^^

SyntaxError: Cannot use import statement outside a module

The setup

import * as uuid_ossp from '@electric-sql/pglite/dist/contrib/uuid_ossp';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PGliteDriver } from 'typeorm-pglite';

console.log(uuid_ossp);

export const getRootTypeormModule = () => {
  return TypeOrmModule.forRoot({
    type: 'postgres',
    driver: new PGliteDriver({ extensions: { uuid_ossp: uuid_ossp.uuid_ossp } })
      .driver,
  });
};

This is my tsconfig

const config = {
  displayName: 'backend',
  restoreMocks: true,
  clearMocks: true,
  resetMocks: true,
  testEnvironment: 'node',
  preset: '../../jest.preset.js',
  globalSetup: '<rootDir>/jest.global-setup.ts',
  setupFiles: [
    'jest-plugin-unhandled-promise/setup',
    '<rootDir>/jest.setup.ts',
  ],
  transform: {
    '^.+\\.spec\\.[tj]s?$': '@swc/jest',
  },
  moduleFileExtensions: ['ts', 'js', 'html'],
  roots: ['<rootDir>', './src/__mocks__'],
  workerIdleMemoryLimit: 0.2,
  setupFilesAfterEnv: ['../../jest.setup-after-env.ts'],
};

SWC config

{
  "$schema": "http://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "transform": { "decoratorMetadata": true, "legacyDecorator": true },
    "loose": false
  }
}

Dont know why I need to import the uuid extensions from /dist (this is not what's shown in the docs)

Also, I tried to add this to jest config

transformIgnorePatterns: [`node_modules/(?!(@electric-sql/pglite)/)`]

and that error goes away but the import is an empty object

seems kind of an issue of ESmodule , but after a long time I didn't find the issue and not sure if it's up to me or some misconfiguration in the packages.

Any clue?

Thanks in advance

sergio-milu avatar Dec 02 '24 12:12 sergio-milu

There are some uuid_ossp tests in packages/pglite/tests/contrib/uuid_ossp.test.js. Does that help?

tdrz avatar Dec 03 '24 09:12 tdrz

Hello,

I’m encountering the same issue as described earlier. Specifically, I’m trying to import uuid_ossp, and the problem seems to originate from @electric-sql/pglite/dist/contrib/uuid_ossp.d.ts. Here are the details:

Primary Issue

When attempting to import uuid_ossp, I get the following error:

  ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { d as PGliteInterface } from '../pglite-CCwqaSmq.js';
                                                                                    ^^^^^^

  SyntaxError: Cannot use import statement outside a module

Attempted Fix

I tried adding the following configuration in my Jest setup:

transformIgnorePatterns: ['node_modules/(?!(@electric-sql/pglite)/)']

However, this resulted in a new error:

TypeError: Cannot read properties of undefined (reading 'setup')

Reproduction Steps

I also attempted to reproduce the issue using the test examples in the pglite repository, specifically in packages/pglite/tests/contrib/uuid_ossp.test.js. I replaced the import with the following:

import { uuid_ossp } from '../../dist/contrib/uuid_ossp.d.ts';

This yielded the same undefined error.

Environment:

  • pglite Version: 0.2.14
  • ts-jest Version : 29.1.1
  • jest Version : 29.7.0

Thank you for investigating this issue. Please let me know if you need more details!

MrKernelPanic-dev avatar Dec 04 '24 10:12 MrKernelPanic-dev

Hello,

I’m encountering the same issue as described earlier. Specifically, I’m trying to import uuid_ossp, and the problem seems to originate from @electric-sql/pglite/dist/contrib/uuid_ossp.d.ts. Here are the details:

Primary Issue

When attempting to import uuid_ossp, I get the following error:

  ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { d as PGliteInterface } from '../pglite-CCwqaSmq.js';
                                                                                    ^^^^^^

  SyntaxError: Cannot use import statement outside a module

Attempted Fix

I tried adding the following configuration in my Jest setup:

transformIgnorePatterns: ['node_modules/(?!(@electric-sql/pglite)/)']

However, this resulted in a new error:

TypeError: Cannot read properties of undefined (reading 'setup')

Reproduction Steps

I also attempted to reproduce the issue using the test examples in the pglite repository, specifically in packages/pglite/tests/contrib/uuid_ossp.test.js. I replaced the import with the following:

import { uuid_ossp } from '../../dist/contrib/uuid_ossp.d.ts';

This yielded the same undefined error.

Environment:

  • pglite Version: 0.2.14
  • ts-jest Version : 29.1.1
  • jest Version : 29.7.0

Thank you for investigating this issue. Please let me know if you need more details!

manage to know what's going on

you must import from @electric-sql/pglite/contrib/uuid_ossp.d.ts not the dist, event if TS complains about not exists, in my case this is cause my repo is not configured for modules yet, and you need to have that in order to be able to understand the exports in the package.json of this project

sergio-milu avatar Dec 05 '24 12:12 sergio-milu

Same problem here :(

dt-alexandre-victoor avatar May 05 '25 08:05 dt-alexandre-victoor

Here what worked for me:

const { uuid_ossp } = require('@electric-sql/pglite/contrib/uuid_ossp');
const pg = new PGlite({
     extensions: { uuid_ossp },
 });

alexvictoor avatar May 17 '25 11:05 alexvictoor