create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Support CJS file extensions

Open joeldenning opened this issue 2 years ago • 27 comments

Currently CRA treats .cjs files as asset/resource files rather than javascript files. See https://nodejs.org/api/packages.html#determining-module-system for explanation of how the .cjs file extension is treated by NodeJS.

I wasn't able to add tests to this PR because all of the tests failed epicly when I ran them locally, after following the instructions in CONTRIBUTING.md and swapping between Node 14, 16, and 17. I manually tested this change on a local create-react-app project to verify that CJS files are now processed as javascript. I expect the lack of tests will make this PR not mergeable, so any guidance on how to fix the many many errors that occur while running npx jest test/ --watchAll is appreciated.

joeldenning avatar Feb 03 '22 16:02 joeldenning

👍 would be great to see this merged, quite a few libraries use .cjs (e.g. @apollo/client), so the current webpack config can cause weird module import failures depending on what you're doing. The alternative is to use something like craco to monkey-patch the webpack config, but that's obviously not ideal.

jenseng avatar Feb 03 '22 17:02 jenseng

@jenseng Second that! This is also affecting the very popular file uploader package uppy and all its sub-packages (@uppy/react, @uppy/dashboard, @uppy/aws-s3, @uppy/drag-drop) to name a few.

paulhklam1122 avatar Mar 13 '22 22:03 paulhklam1122

This PR successfully fixes the issue for my project. Shouldn't this be prioritised, @iansu? Looks like a common issue.

felschr avatar Mar 29 '22 08:03 felschr

Would love if this gets merged in as it solves issues with a lot of libs

ndrabins avatar Apr 03 '22 20:04 ndrabins

Would be great to have this merged in.

In the meantime, it's possible to continue using .cjs modules with react-app-rewired:

// config-overrides.js
module.exports = {
  webpack: function (config, env) {
    config.module.rules = config.module.rules.map(rule => {
      if (rule.oneOf instanceof Array) {
        rule.oneOf[rule.oneOf.length - 1].exclude = [/\.(js|mjs|jsx|cjs|ts|tsx)$/, /\.html$/, /\.json$/];
      }
      return rule;
    });
    return config;
  },
}

nyan-left avatar Apr 25 '22 11:04 nyan-left

Guys, any update on this ?

Ionut-Milas avatar May 03 '22 14:05 Ionut-Milas

This solved our issue in https://github.com/solana-labs/oyster/issues/538 - it would be great to have this merged! :pray:

aaronovz1 avatar Jun 02 '22 08:06 aaronovz1

Any update? This is a blocker!

weidonglian avatar Jun 02 '22 16:06 weidonglian

Looking forward to someone finally fixing this.

zxl777 avatar Jun 04 '22 01:06 zxl777

We need this as well and it's really important for our team, @uppy doesn't work without a .cjs support and potentially @apollo/client in the future. Please review this and merge.

nyatskiv avatar Jun 16 '22 12:06 nyatskiv

Hey @nyatskiv I got uppy working by doing the following as a stopgap before they merge this

Install these packages

npm i react-app-rewired --save-dev
npm i customize-cra  --save-dev

Update your build and start scripts

  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
  }

Create a config-overrides.js file with the following contents

//@ts-ignore
const {
  override,
  addWebpackModuleRule,
  setWebpackTarget,
} = require("customize-cra");

module.exports = override(
  addWebpackModuleRule({
    test: /\.(cjs)$/,
    exclude: /@babel(?:\/|\\{1,2})runtime/,
    loader: require.resolve("babel-loader"),
    options: {
      babelrc: false,
      configFile: false,
      compact: false,
      presets: [
        [
          require.resolve("babel-preset-react-app/dependencies"),
          { helpers: true },
        ],
      ],
      cacheDirectory: true,
      cacheCompression: false,
      sourceMaps: true,
      inputSourceMap: true,
    },
  })
);

nigelheap avatar Jun 16 '22 12:06 nigelheap

+1 Hoping this will be reviewed and merged. This fixes an issue I'm having and I believe this would fix https://github.com/facebook/create-react-app/issues/12155.

vezwork avatar Sep 07 '22 19:09 vezwork

    config.module.rules = config.module.rules.map(rule => {
      if (rule.oneOf instanceof Array) {
        rule.oneOf[rule.oneOf.length - 1].exclude = [/\.(js|mjs|jsx|cjs|ts|tsx)$/, /\.html$/, /\.json$/];
      }
      return rule;
    });

You are my new favorite internet stranger @nyan-left! I've spent all afternoon fighting with this trying to figure out why my dependency importing axios wasn't working!

Thanks a lot, hopefully this can get merged in to fix it properly ASAP.

rcbevans avatar Dec 06 '22 00:12 rcbevans

I also would love for this to be reviewed and merged. 🙏

deivamagalhaes avatar Feb 10 '23 18:02 deivamagalhaes

    config.module.rules = config.module.rules.map(rule => {
      if (rule.oneOf instanceof Array) {
        rule.oneOf[rule.oneOf.length - 1].exclude = [/\.(js|mjs|jsx|cjs|ts|tsx)$/, /\.html$/, /\.json$/];
      }
      return rule;
    });

You are my new favorite internet stranger @nyan-left! I've spent all afternoon fighting with this trying to figure out why my dependency importing axios wasn't working!

Thanks a lot, hopefully this can get merged in to fix it properly ASAP.

Same here, importing [email protected] as require('axios') inside a dependency is not working. It's returning some path string instead of the actual module, causing axios_1.default to be undefined.

Screen Shot 2023-02-28 at 13 29 39

I could work around this by adding CRACO to my project, with the configuration suggested here: https://github.com/facebook/create-react-app/issues/11889#issuecomment-1114928008.

Is there any estimated ETA for merging this PR, so we can just use latest react-scripts and axios versions out of the box?

tmilar avatar Feb 28 '23 12:02 tmilar

This PR has been kicking around for a year, and it clearly solves a known issue. What's blocking the progress here? 😖

It, and several other PRs doing the same thing, seem like they would effectively solve https://github.com/facebook/create-react-app/issues/12700.

laustdeleuran avatar Mar 09 '23 16:03 laustdeleuran

Ran into this issue today, I'm glad I was able to eventually find this PR and learn about the root cause + workarounds. But like the previous commenter, I'm also wondering what is blocking the PR for so long. This seems like a good solution for an issue that has been reported multiple times by now.

maartenverbaarschot avatar Mar 20 '23 12:03 maartenverbaarschot

I think we need to give up on create react app, it looks like they are not supporting it anymore with the new docs anyway.

If you have a look at my comment from 9 months ago there is a way to use rewire to solve the issue

nigelheap avatar Mar 20 '23 12:03 nigelheap

it looks like they are not supporting it anymore with the new docs anyway.

@nigelheap, can you expand on what you mean? Or link to where in the docs it says that they are not supporting (and what they're note supporting)?

laustdeleuran avatar Mar 20 '23 21:03 laustdeleuran

@nigelheap, can you expand on what you mean? Or link to where in the docs it says that they are not supporting (and what they're note supporting)?

@laustdeleuran The new react docs don't mention using create react app. https://react.dev/learn/start-a-new-react-project basically says to use a framework or use Vite or Percel.

Specific part about not using a framework https://react.dev/learn/start-a-new-react-project#can-i-use-react-without-a-framework

nigelheap avatar Mar 20 '23 22:03 nigelheap

Hello from 2023, burned a few hours on this frustrating issue! Any chance of getting this merged?

TinyCamera avatar Jul 08 '23 01:07 TinyCamera

I'm also having this issue.

deivamagalhaes avatar Jul 12 '23 19:07 deivamagalhaes

@zpao sorry for the direct hit. But can you help merging this? Thank you

djejaquino avatar Jul 27 '23 18:07 djejaquino

It is sad that this has not been sorted after 1 year of collective frustration - what is up?

javaspeak avatar Sep 13 '23 07:09 javaspeak

I'm still hopping this gets merge :/

hugocostadev avatar Dec 05 '23 20:12 hugocostadev

This issue is still breaking otherwise correctly written packages used within CRA projects (e.g. https://github.com/101arrowz/fflate/issues/193). .cjs has been part of Node's module resolution system for a few years now, so I'd have hoped CRA would support it. Is there anything else that needs to be done for this to be merged?

101arrowz avatar Feb 05 '24 17:02 101arrowz

My take here is that CRA is now abandonware and we should either eject, or migrate to other providers like vite.

nyan-left avatar May 23 '24 11:05 nyan-left