Repository navigation
Add support for CJS files #12700
Description
Activity
This breaks a few different packages for me. I need to go in and directly modify the
webpack.config.jsin order to fix it. It'd be really helpful if this could get some attention. Until then, CRA needs to be forked...This issue was referenced back in 2020, but was closed as completed for some reason: #8754
Reacted by Brandon Zarrella, Joseph Kohlmann and Laust DeleuranFor anyone who needs CJS support, add
react-app-rewiredand add a file namedconfig-overrides.jsat the root of your project, with the following contents:const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier'); const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false'; module.exports = function override(config, webpackEnv) { console.log('overriding webpack config...'); const isEnvDevelopment = webpackEnv === 'development'; const isEnvProduction = webpackEnv === 'production'; const loaders = config.module.rules[1].oneOf; loaders.splice(loaders.length - 1, 0, { test: /\.(js|mjs|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, // See #6846 for context on why cacheCompression is disabled cacheCompression: false, // @remove-on-eject-begin cacheIdentifier: getCacheIdentifier( isEnvProduction ? 'production' : isEnvDevelopment && 'development', [ 'babel-plugin-named-asset-import', 'babel-preset-react-app', 'react-dev-utils', 'react-scripts', ] ), // @remove-on-eject-end // Babel sourcemaps are needed for debugging into node_modules // code. Without the options below, debuggers like VSCode // show incorrect code and set breakpoints on the wrong lines. sourceMaps: shouldUseSourceMap, inputSourceMap: shouldUseSourceMap, }, }); return config; };
Then replace your the
react-scriptsscripts in yourpackage.jsonwith the following:"start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test",Fixed it for me.
Reacted by Joseph Kohlmann, Rob Cochran and Jessie WeiReacted by JimmyFor anyone who needs CJS support, add
react-app-rewiredand add a file namedconfig-overrides.jsat the root of your project, with the following contents:I would recommend using https://github.com/dilanx/craco/ instead, it is better maintained and provide more flexibility.
Reacted by Joseph KohlmannI can confirm the issue and here's the work around to prove it, would be good to have CRA updated to include CJS.
When I change webpack.config.js (adding cjs to the mix) within the node_modules of my CRA app I no longer receive undefined error when loading my dependency which uses .cjs.
// Process application JS with Babel. // The preset includes JSX, Flow, TypeScript, and some ESnext features. { test: /\.(js|mjs|jsx|ts|tsx|cjs)$/,// Process any JS outside of the app with Babel. // Unlike the application JS, we only compile the standard ES features. { test: /\.(js|mjs|cjs)$/,I adapted @nyan-left's workaround for use with
craco, and it solved my issues:// craco.config.ts export default { webpack: { configure: config => ({ ...config, module: { ...config.module, rules: config.module.rules.map(rule => { if (rule.oneOf instanceof Array) { // eslint-disable-next-line no-param-reassign rule.oneOf[rule.oneOf.length - 1].exclude = [ /\.(js|mjs|jsx|cjs|ts|tsx)$/, /\.html$/, /\.json$/, ]; } return rule; }), }, }), }, };
However, I still think this is a bug in
create-react-app, that this issue is valid, and that one of the many PRs mentioned in this issue would be a better way to solve it.Reacted by Vladimir Honcharenko, Gabriel Ramos Takeda, Sarkazein, Iddriss Raaj, Domagoj Cerjan, James Stuart Milne, Juraj Kavka, Gabriel Jablonski, Sean Newell, Rudomanenko Volodymyr and 7 moreReacted by AladdainReacted by Gabriel Ramos Takeda, Sarkazein and Artsiom DubinevichHi. I spent couple of hours with solving nothing expanatory error message. I was trying to use my package with dependency on
react-tooltipwithin host project built withcreate-react-appFollowing workaround above, my project works, but we have like 20 projects depended on our custom package and all of them are built with create-react-app.
- added 2 commits that reference this issue
on Nov 25, 2024

Describe the bug
If a dependency contains a
.cjsfile, then said file is not processed by babel due to the default webpack config, meaning it will not be loaded.Also, some
cjsfile are not excluded when needed: #11889Did you try recovering your dependencies?
Tried but did not help
Which terms did you search for in User Guide?
CJS, ESM, commonjs
Environment
Steps to reproduce
Use any npm package that contains
*.cjsfiles.Expected behavior
The
.cjsfiles are correctly loaded and the functions, variable, classes, defined in them are available.Actual behavior
The
.cjsfiles notloaded and the functions, variable, classes, defined in them areundefined.Reproducible demo
I don't believe this is necessary as there at least 5 PRs opened to fix this issue:
Related issues