nanoevents: Importing error in React Native starting from nanoevents 3.0 version
Because .cjs
source extension is not very commonly used and React Native do not know it and interprets as part of filename.
error: bundling failed: Error: While trying to resolve module `nanoevents` from file `.../src/api/ws/client.js`, the package `.../node_modules/nanoevents/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`.../node_modules/nanoevents/index.cjs`. Indeed, none of these files exist:
* .../node_modules/nanoevents/index.cjs(.native|.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)
* .../node_modules/nanoevents/index.cjs/index(.native|.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)
Workarounds:
import {createNanoEvents} from 'nanoevents/index'
- add
.cjs
extension tometro.config.js
:
const {getDefaultValues} = require('metro-config/src/defaults')
const {resolver: {sourceExts}} = getDefaultValues()
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
resolver: {
sourceExts: [...sourceExts, 'cjs'],
},
}
But it will be nice nanoevents package do not cause the error and works without need an additional configuration.
- Use
index.cjs.js
file name instead - Or add
react-native
field topackage.json
withindex.js
value
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (12 by maintainers)
Commits related to this issue
- fix: export package.json required by react-native and bundlers It appears that react-native and some bundlers rely on being able to introspect the package.json file of a npm module. After adding the ... — committed to uuidjs/uuid by ctavan 4 years ago
- fix: export package.json required by react-native and bundlers It appears that react-native and some bundlers rely on being able to introspect the package.json file of a npm module. After adding the ... — committed to uuidjs/uuid by ctavan 4 years ago
- fix: export package.json required by react-native and bundlers It appears that react-native and some bundlers rely on being able to introspect the package.json file of a npm module. After adding the ... — committed to uuidjs/uuid by ctavan 4 years ago
- fix: export package.json required by react-native and bundlers It appears that react-native and some bundlers rely on being able to introspect the package.json file of a npm module. After adding the ... — committed to uuidjs/uuid by ctavan 4 years ago
- fix: export package.json required by react-native and bundlers (#449) It appears that react-native and some bundlers rely on being able to introspect the package.json file of a npm module. After add... — committed to uuidjs/uuid by ctavan 4 years ago
@ai @farwayer @TrySound I would be curious to learn from you if you think
package.json
should be included in allpkg.exports
fields or whether this is a “bug” in react-native: https://github.com/react-native-community/cli/issues/1168@farwayer thanks for fact that React Native overwrite
resolverMainFields
.I simplify React Native support with native ESM and without extra files https://github.com/ai/dual-publish/commit/9d8b0733a7bab937cddac4c7e537f12648a41cde
New Dual Publish and Nano Events versions were published.
@ai fix “csj” in metro issue
We need
.cjs
name, because otherwise, Node.js will load it as ES module.Most of the npm packages do not provide ESM for Node.js. Most of the packages provide ESM for webpack only, which does not follow the spec.
Yeap, it is part of Node.js standard.
Yes, we should start by creating an issue in the metro builder. Can you do it since I am less familiar with the React Native ecosystem?
I do not like “it is your tool” problem answer in the issue. You have a real problem and we need a solution.
Right now I see this solution:
index.mjs
instead ofindex.js
index.js
instead ofindex.cjs
type: 'module'
frompackage.json
index.mjs
onrequire('nanoevents/index.js')
.Unfortunately, we need to support all kind of ESM environments:
Unfortunately the
index.js → index.mjs
fix will force all CDN users to manually addindex.mjs
to the URLs. I will think about the better solution today and, if I will not find the best solution, I will think about who we should suffer in the situation.