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

.cjs does not work with React Native and/or Expo #233

Closed
efoken opened this issue Jul 16, 2020 · 14 comments · Fixed by #238
Closed

.cjs does not work with React Native and/or Expo #233

efoken opened this issue Jul 16, 2020 · 14 comments · Fixed by #238

Comments

@efoken
Copy link

efoken commented Jul 16, 2020

Since the latest version, which changed the file extension to .cjs, it does not work anymore with React Native. I'm getting the following error:

Failed building JavaScript bundle.
While trying to resolve module `stylis` from file `/Users/.../node_modules/@emotion/cache/dist/cache.browser.cjs.js`, the package `/Users/.../node_modules/stylis/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/Users/.../node_modules/stylis/dist/stylis.cjs`. Indeed, none of these files exist:

  * /Users/.../node_modules/stylis/dist/stylis.cjs(.native|.ios.expo.ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx|.expo.tsx|.ios.expo.js|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.wasm|.native.wasm|.wasm)
  * /Users/.../node_modules/stylis/dist/stylis.cjs/index(.native|.ios.expo.ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx|.expo.tsx|.ios.expo.js|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.wasm|.native.wasm|.wasm)
@efoken efoken changed the title Does not work anymore with React Native and/or Expo .cjs does not work anymore with React Native and/or Expo Jul 16, 2020
@efoken efoken changed the title .cjs does not work anymore with React Native and/or Expo .cjs does not work with React Native and/or Expo Jul 16, 2020
@Andarist
Copy link
Collaborator

It really seems like the Metro bundler/Expo problem. While we could "fix" this on our side this should definitely be fixed on theirs as .cjs will become more common in the ecosystem in the future.

@sanpoChew
Copy link

sanpoChew commented Jul 24, 2020

there's an issue open on the metro repo facebook/metro#535

builds work fine again for me after adding this to my metro config:

  resolver: {
    sourceExts: ['js', 'json', 'ts', 'tsx', 'cjs'],
  },

voidrender added a commit to voidrender/demo-react-native-emotion that referenced this issue Sep 2, 2020
Workaround for metro deficiency: facebook/metro#535

Caused by stylis shipping a .cjs file: thysultan/stylis#233

Fix from emotion-js/emotion#1986
voidrender added a commit to voidrender/demo-react-native-emotion that referenced this issue Sep 2, 2020
Workaround for metro deficiency: facebook/metro#535

Caused by stylis shipping a .cjs file: thysultan/stylis#233

Fix from emotion-js/emotion#1986
@hueter
Copy link

hueter commented Sep 2, 2020

Unfortunately none of the work-arounds floating about are working for me on an Expo project, so this issue is blocking the upgrade to Emotion 11.

It makes sense to me that it's a Metro / Expo problem, but I haven't been able to find any indication that they intend to fix it on their end or that it's even on their radar since facebook/metro#535 hasn't gotten any responses 😞

@efoken
Copy link
Author

efoken commented Sep 4, 2020

@hueter for Expo you have to change your app.json file:

{
  "expo": {
    ...
    "packagerOpts": {
      "sourceExts": ["cjs"]
    }
}

@hueter
Copy link

hueter commented Sep 4, 2020

@efoken oh nice, that just worked. Thank you. I couldn't find docs for that anywhere but I'm glad it was that easy

@kasselTrankos
Copy link

Hi, all
im loading fluture tah uses index.cjs, and even doing
"packagerOpts": {
"sourceExts": ["cjs"]
}
doesnt works, any idea?
thanks

sounisi5011 added a commit to sounisi5011/react-native-test--using-sounisi5011-encrypted-archive that referenced this issue Dec 12, 2021
@sounisi5011/ts-utils-is-property-accessible uses the `.cjs` extension.
It may not work because of this.
see thysultan/stylis#233 (comment)
@smuralimohan
Copy link

Hi, all im loading fluture tah uses index.cjs, and even doing "packagerOpts": { "sourceExts": ["cjs"] } doesnt works, any idea? thanks

If you are using expo SDK >41 try this metro.config.js(create the file if it does not exist) in your root folder.

const { getDefaultConfig } = require('expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts.push('cjs');
module.exports = defaultConfig;

@Asantie
Copy link

Asantie commented Jan 15, 2022

I tried adding a metro config as below

const { getDefaultConfig } = require('expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts.push('cjs');
module.exports = defaultConfig;

well it works but its brings errors when I use moti.fly... reanimated error.

any fix on this issue??

@cybercris
Copy link

cybercris commented Feb 3, 2022

I tried adding a metro config as below

const { getDefaultConfig } = require('expo/metro-config'); const defaultConfig = getDefaultConfig(__dirname); defaultConfig.resolver.sourceExts.push('cjs'); module.exports = defaultConfig;

well it works but its brings errors when I use moti.fly... reanimated error.

any fix on this issue??

nandorojo/moti#141 (comment) @Asantie

@benjaminrittenhouse
Copy link

If you are using expo SDK >41 try this metro.config.js(create the file if it does not exist) in your root folder.

This fixed the issue for me. I looked everywhere and this finally got my iOS build working. Anyone with same issue, ensure you have file in root folder (metro.config.js) and use this code with extension that is not recognized ( in my case it was cjs). Thank you for this solution!

@doltee
Copy link

doltee commented May 11, 2022

This fiexed the issue for me too.

I tried to fix this finally got my expo Android build working.
I created metro.config.js to root folder,
const { getDefaultConfig } = require('expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts.push('cjs');
module.exports = defaultConfig;

I fixed the error.

Thank you.

Error message is
"
Android Bundling failed 1905ms
Unable to resolve module idb from C:\Users\kkk\Desktop\sparta_study\sparta-myhoneytip-jin\node_modules@firebase\app\dist\esm\index.esm2017.js: idb could not be found within the project or in these directories:
node_modules@firebase\app\node_modules
node_modules
..\node_modules
821 | }
822 | else {

823 | const idbHeartbeatObject = await readHeartbeatsFromIndexedDB(this.app);
| ^
824 | return idbHeartbeatObject || { heartbeats: [] };
825 | }
826 | }

@harshit0571
Copy link

mines still not working

can i get the documentation on 9.6.11 version so i can use different version

@rehanganapathy
Copy link

mine isn't working even after adding the metro.config.js file

@brailecg
Copy link

Hi, all im loading fluture tah uses index.cjs, and even doing "packagerOpts": { "sourceExts": ["cjs"] } doesnt works, any idea? thanks

If you are using expo SDK >41 try this metro.config.js(create the file if it does not exist) in your root folder.

const { getDefaultConfig } = require('expo/metro-config'); const defaultConfig = getDefaultConfig(__dirname); defaultConfig.resolver.sourceExts.push('cjs'); module.exports = defaultConfig;

This fixed if for me as well. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.