Error "ReferenceError: window is not defined" with React Native Web version.
lucaskraus opened this issue · 2 comments
lucaskraus commented
What happened?
I'm trying to use the Web version of my project, but the bundle just stop to running immediately after complete the process with the following error in exit:
/home/lucas/code/Frontend/node_modules/@react-native-async-storage/async-storage/lib/commonjs/AsyncStorage.js:63
return createPromise(() => window.localStorage.getItem(key), callback);
^
ReferenceError: window is not defined
Version
2.0.0
What platforms are you seeing this issue on?
- Android
- iOS
- macOS
- Windows
- web
System Information
System:
OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
Memory: 9.21 GB / 15.51 GB
Shell:
version: 5.1.16
path: /bin/bash
Binaries:
Node:
version: 20.15.1
path: ~/.nvm/versions/node/v20.15.1/bin/node
Yarn: Not Found
npm:
version: 10.7.0
path: ~/.nvm/versions/node/v20.15.1/bin/npm
Watchman: Not Found
SDKs:
Android SDK: Not Found
IDEs:
Android Studio: Not Found
Languages:
Java:
version: javac 22
path: /home/lucas/.sdkman/candidates/java/current/bin/javac
Ruby: Not Found
npmPackages:
"@react-native-community/cli": Not Found
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.73.6
wanted: 0.73.6
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: Not found
newArchEnabled: Not found
iOS:
hermesEnabled: Not found
newArchEnabled: Not found
Steps to Reproduce
- create a new project
- install @react-native-async-storage/async-storage version 2.0.0
- try to run web version
hoahv20 commented
same issue. I was downgrade to version 1.23.1 but it's still occur
Undertone0809 commented
I solve it by the following code.
import { AppState, Platform } from 'react-native';
import 'react-native-url-polyfill/auto';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = process.env.EXPO_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.EXPO_PUBLIC_SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseAnonKey) {
throw new Error('Supabase URL or Anon Key is not defined');
}
class SupabaseStorage {
async getItem(key: string) {
if (Platform.OS === "web") {
if (typeof localStorage === "undefined") {
return null;
}
return localStorage.getItem(key);
}
return AsyncStorage.getItem(key);
}
async removeItem(key: string) {
if (Platform.OS === "web") {
return localStorage.removeItem(key);
}
return AsyncStorage.removeItem(key);
}
async setItem(key: string, value: string) {
if (Platform.OS === "web") {
return localStorage.setItem(key, value);
}
return AsyncStorage.setItem(key, value);
}
}
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
auth: {
storage: new SupabaseStorage(),
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: false,
},
});
ref issue: supabase/supabase-js#786