question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mjs module resolution for React Native/Metro is broken

See original GitHub issue
error While trying to resolve module `use-context-selector` from file 
`/Users/groza/Work/welearn/welearn-app/src/components/UI/video-player/VideoPlayerController.tsx`, the package `/Users/groza/Work/welearn/welearn-app/node_modules/use-context-selector/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/Users/groza/Work/welearn/welearn-app/node_modules/use-context-selector/dist/index.native.modern.js`. Indeed, none of these files exist:

  * /Users/groza/Work/welearn/welearn-app/node_modules/use-context-selector/dist/index.native.modern.js(.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.mjs|.native.mjs|.mjs)
  * /Users/groza/Work/welearn/welearn-app/node_modules/use-context-selector/dist/index.native.modern.js/index(.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.mjs|.native.mjs|.mjs).

my metro.config.js (i’ve already added mjs here):

const defaultSourceExts = require('metro-config/src/defaults/defaults').sourceExts;

module.exports = {
    transformer: {
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: true,
            },
        }),
    },
    resolver: {
        sourceExts: process.env.RN_SRC_EXT
            ? [...process.env.RN_SRC_EXT.split(',').concat(defaultSourceExts), 'mjs']
            : [...defaultSourceExts, 'mjs'],
    },
};

manually changing "react-native": "./dist/index.native.modern.js", to "react-native": "./dist/index.native.modern.mjs", or "react-native": "./dist/index.native.modern", allows the build to go through

same thing for manually renaming index.native.modern.mjs to index.native.modern.js

RN version 0.68.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

0reactions
dai-shicommented, Jun 7, 2022

@sf185127 Thanks for confirming!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add .cjs and .mjs files support · Issue #535 · facebook/metro
Error: While trying to resolve module `lib` from file `project/index.js`, the package `project/node_modules/lib/package.json` was ...
Read more >
Metro Bundler fails while trying to resolve module `immer ...
mjs file but no H:\my_project\app\node_modules\immer\dist\immer.esm.mjs\ folder with an index file in it. So I tried to manually remove ...
Read more >
Module Resolution | Metro - Meta Open Source
Module resolution is the process of translating module names to module paths at build time. For example, if your project contains the code:...
Read more >
Plug'n'Play | Yarn - Package Manager
An overview of Plug'n'Play, a powerful and innovative installation strategy for Node.
Read more >
What does it take to support Node.js ESM? – The Guild
js , set "type": "module" , and the CommonJS version of your modules with the .cjs extension. Clearly using the .mjs extension is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found