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

SyntaxError: Cannot use import statement outside a module #1349

Closed
ranouf opened this issue Jan 8, 2025 · 2 comments
Closed

SyntaxError: Cannot use import statement outside a module #1349

ranouf opened this issue Jan 8, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@ranouf
Copy link

ranouf commented Jan 8, 2025

Hi,

I'm new with supabase and supabase-js.

I'm trying to validate my polices working with supabase-js testings.

I would like to use "import" instead of "require" but I have an error:
SyntaxError: Cannot use import statement outside a module

How to enable "import"?

Here is the packages.json:

{
  "name": "supabase-tests",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "supabase:env": "npx supabase status -o env > .env",
    "supabase:start": "npx supabase start",
    "test": "jest",
    "test:watch": "jest --watch"
  },
  "dependencies": {
    "@supabase/supabase-js": "^2.0.0",
    "dotenv": "^16.4.7"
  },
  "devDependencies": {
    "@types/jest": "^29.0.0",
    "jest": "^29.0.0",
    "ts-jest": "^29.0.0"
  }
}

supabaseClient.ts

import { createClient } from "@supabase/supabase-js";
require('dotenv/config');

const { SERVICE_ROLE_KEY, API_URL } = process.env;

if (!SERVICE_ROLE_KEY || !API_URL) {
    throw new Error('Missing environment variables. Verify supabase is started and environment variables are set.');
}


// Connect to the local Supabase instance
const supabaseUrl = process.env.SUPABASE_URL || API_URL;
const supabaseKey = process.env.SUPABASE_ANON_KEY || SERVICE_ROLE_KEY;
export const supabase = createClient(supabaseUrl, supabaseKey);

api.test.ts:

import { supabase } from "./supabaseClient";


test('Fetch users from local Supabase instance', async () => {
    const { data, error } = await supabase.from('users').select('*');
    expect(error).toBeNull();
    expect(data).toBeInstanceOf(Array);
});
@ranouf ranouf added the bug Something isn't working label Jan 8, 2025
@kamilogorek
Copy link
Member

You need to preconfigure jest in order for it to understand typescript compilation.
See: https://jestjs.io/docs/getting-started#using-typescript and https://jestjs.io/docs/ecmascript-modules

Or better yet use https://vitest.dev/guide/ which supports both typescript and esm out of the box :)

@ranouf
Copy link
Author

ranouf commented Jan 8, 2025

vitest was the solution! Thanks @kamilogorek !

@ranouf ranouf closed this as completed Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants