Unable to use in React Native due to import.meta not being supported #1967
Replies: 14 comments 66 replies
-
If import.meta isn't supported, it means ESM isn't supported, I guess. Our intentional workaround is to use "module" condition instead of "import" condition, but I'm afraid Hermes doesn't support those conditions in the first place. 🤔 @barelyhuman Do we have similar issues reported? We use the same config across zustand/jotai/valtio, and I suspect there are RN users who use Hermes... |
Beta Was this translation helpful? Give feedback.
-
Coming here as a user of Jotai but having ran into the same issue. First off, this issue is only apparent when using the metro config From here, metro runs by the A fix for expo users with
Patching my local version, i've confirmed this to work on local projects. Note that while i've veered away from turning on |
Beta Was this translation helpful? Give feedback.
-
I was able to make it work for RN I've just removed elm exports from package json, without any changes in metro config (I had conflicts with different libraries).
|
Beta Was this translation helpful? Give feedback.
-
Setting unstable_transformImportMeta to true in babel config solves the problem. (tested with expo sdk 53 beta) babel.config.js module.exports = function (api) {
api.cache(true);
return {
presets: [["babel-preset-expo", { unstable_transformImportMeta: true }]],
};
}; |
Beta Was this translation helpful? Give feedback.
-
backlink pmndrs/jotai#3041 |
Beta Was this translation helpful? Give feedback.
-
I've successefully make a workaround for this problem 😎. The previous answers only works in dev builds for me 😢. if you run the command: grep -r "import.meta" node_modules/zustand will return this: node_modules/zustand/esm/middleware.mjs: extensionConnector = (enabled != null ? enabled : (import.meta.env ? import.meta.env.MODE : void 0) !== "production") && window.__REDUX_DEVTOOLS_EXTENSION__;
node_modules/zustand/esm/middleware.mjs: if ((import.meta.env ? import.meta.env.MODE : undefined) !== "production" && a[0].type === "__setState" && !didWarnAboutReservedActionType) { I replaced those expressions with /* eslint-disable @typescript-eslint/no-require-imports */
const fs = require('fs')
const path = require('path')
const filePath = path.resolve(
__dirname,
'../node_modules/zustand/esm/middleware.mjs'
)
if (!fs.existsSync(filePath)) {
console.error('Zustand middleware.mjs not found!')
process.exit(1)
}
let content = fs.readFileSync(filePath, 'utf8')
content = content.replace(
/\(import\.meta\.env\s?\?\s?import\.meta\.env\.MODE\s?:\s?(void 0|undefined)\)\s?!==\s?"production"/g,
'__DEV__'
)
fs.writeFileSync(filePath, content, 'utf8')
console.log('✅ Zustand patched successfully!') In package.json: "scripts": {
"postinstall": "node scripts/patch-zustand.js"
} Hope this helps someone else out there dealing with this! |
Beta Was this translation helpful? Give feedback.
-
Seeing this issue pop up after I upgraded to Expo 53 |
Beta Was this translation helpful? Give feedback.
-
Has anyone tried https://github.com/pmndrs/zustand/releases/tag/v5.0.4 ? |
Beta Was this translation helpful? Give feedback.
-
Got this error after upgrade expo 53. Any solution? |
Beta Was this translation helpful? Give feedback.
-
Unfortunately For pure react-native projects we have to patch the files? |
Beta Was this translation helpful? Give feedback.
-
y babel.config.js configuration, this is how it was solved in my case, I hope it works for them. |
Beta Was this translation helpful? Give feedback.
-
This solved my issue:
I'm on Expo SDK 53, React 19, React-Native 0.79 and Jotai 2.12 |
Beta Was this translation helpful? Give feedback.
-
Here's my solution, overriding the resolution to CommonJS resolution in const { getDefaultConfig } = require("expo/metro-config");
let config = getDefaultConfig(__dirname, {
// [Web-only]: Enables CSS support in Metro.
isCSSEnabled: true,
});
config.resolver.resolveRequest = (context, moduleName, platform) => {
if (moduleName.includes("zustand")) {
const result = require.resolve(moduleName); // gets CommonJS version
return context.resolveRequest(context, result, platform);
}
// otherwise chain to the standard Metro resolver.
return context.resolveRequest(context, moduleName, platform);
};
module.exports = config; |
Beta Was this translation helpful? Give feedback.
-
its still doesnt solve my error, please give me more reference :( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
React Native's Hermes JavaScript engine doesn't support import.meta and exits with an error at compile time, so testing for its existence in code doesn't work. I'm tempted to file a bug for this against Hermes but wondered whether any way around this already existed for Zustand?
Beta Was this translation helpful? Give feedback.
All reactions