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.

Add .cjs and .mjs files support

See original GitHub issue

Do you want to request a feature or report a bug?

bug

What is the current behavior?

If you have require('./index.cjs') you will have issue:

Error: While trying to resolve module `lib` from file `project/index.js`, the package `project/node_modules/lib/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`project/node_modules/lib/index.cjs`. Indeed, none of these files exist:

      * project/node_modules/lib/index.cjs(.native|.web.js|.native.js|.js|.web.json|.native.json|.json|.web.ts|.native.ts|.ts|.web.tsx|.native.tsx|.tsx)
      * project/node_modules/lib/index.cjs/index(.native|.web.js|.native.js|.js|.web.json|.native.json|.json|.web.ts|.native.ts|.ts|.web.tsx|.native.tsx|.tsx)

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.

  1. Create empty project
  2. Add lib.cjs
  3. Create index.js with require('./lib.cjs')

What is the expected behavior?

Load .cjs files as any .js files.

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.

  • No Metro config
  • Metro 0.59.0
  • Node 13.11.0
  • Yarn 1.22.4

Extra details

I opened this issue because users of my dual CJS/ESM project have a problem with using my lib to React Native https://github.com/ai/nanoevents/issues/44#issuecomment-601722303

You had .mjs issue before, but it was closed because in 2017 it was not clear about .mjs standard. Now .mjs and .cjs is an official Node.js API.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:78
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

51reactions
rossmartincommented, Nov 16, 2021

I wanted to chime in and mention how I worked around this currently with RN 0.65.1

metro.config.js

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */
const defaultSourceExts =
  require('metro-config/src/defaults/defaults').sourceExts;
module.exports = {
  transformer: {
    getTransformOptions: () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: true,
      },
    }),
  },
  resolver: {
    sourceExts: process.env.RN_SRC_EXT
      ? [...process.env.RN_SRC_EXT.split(',').concat(defaultSourceExts), 'cjs'] // <-- cjs added here
      : [...defaultSourceExts, 'cjs'], // <-- cjs added here
  },
};

The latest version of @apollo/client (3.5.2) is using .mjs as the entry point of the package. The above change fixed the issue with the dep.

14reactions
huntiecommented, Jul 28, 2022

This issue is resolved in c1c6d9c which should ship in the next major release (0.72.0) 🙂.

  • Metro will now resolve .cjs and .mjs extensions by default when imported explicitly (import/require that includes the module file extension).
  • Additional source file extensions for explicit resolution can now be configured via the watcher.additionalExts option.
  • Importing .cjs and .mjs files implicitly (without giving the extension) can still be configured via resolver.sourceExts.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Publish ESM and CJS in a single package - Anthony Fu
A short tutorial of shipping both ESM and CJS dual formats in a single ... Experimental support of native ESM is introduced in...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
Node.js supports two extensions to help with this: .mjs and .cjs . .mjs files are always ES modules, and .cjs files are always...
Read more >
How to Create a Hybrid NPM Module for ESM and CommonJS.
Creating a single NPM module that works in all environments.
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and ......
Read more >
How we employed the new ECMAScript Module Support in ...
You can also exempt files from this behavior by using special extensions. .mts and .cts are the TS equivalents of .cjs and .mjs...
Read more >

github_iconTop Related Medium Post

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