-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CRA5 Bug: require
a submodule of a package (i.e. my-package/sub-module
) returns a string instead of the module
#11889
Comments
require
a submodule of a package (i.e. my-package/sub-module
) returns a string instead of the modulerequire
a submodule of a package (i.e. my-package/sub-module
) returns a string instead of the module
I also have this issue, any workaround for this? |
same here |
same issue |
Same issue |
same issue |
Impacted as well. |
I think I found the issue. In order to test different webpack configurations, I ejected then directly modified If you change the rule as follows, it works (
|
In the very short term, a workaround is to use craco in order to add the exclusion. Your module.exports = {
webpack: {
configure: (config) => {
// ...
const fileLoaderRule = getFileLoaderRule(config.module.rules);
if(!fileLoaderRule) {
throw new Error("File loader not found");
}
fileLoaderRule.exclude.push(/\.cjs$/);
// ...
return config;
}
}
};
function getFileLoaderRule(rules) {
for(const rule of rules) {
if("oneOf" in rule) {
const found = getFileLoaderRule(rule.oneOf);
if(found) {
return found;
}
} else if(rule.test === undefined && rule.type === 'asset/resource') {
return rule;
}
}
} |
I guess this pull request should also fix this issue: |
This fixes issues with the commonly-used create-react-app tool that is unable to handle .cjs files. This change consider CJS by default for .js files, but still exposes ESM for the tools that have proper support for it. See facebook/create-react-app#11889
This fixes issues with the commonly-used create-react-app tool that is unable to handle .cjs files. This change consider CJS by default for .js files, but still exposes ESM for the tools that have proper support for it. See facebook/create-react-app#11889 Closes comunica/comunica#1097
Same here, importing [email protected] as I could work around this by adding CRACO to my project, with the configuration suggested above: #11889 (comment) (thanks @gdethier!) Is there any estimated ETA for solving this, so we can just use latest react-scripts and axios versions out of the box? |
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated (#147) Lint fixes for prior commit The lint fixes for prior commit
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated (#147) Lint fixes for prior commit The lint fixes for prior commit
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated (#147) Lint fixes for prior commit The lint fixes for prior commit
Updates node to 18, webpack to v8 and addresses security updates ``` nvm install 18 nvm use 18 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force` Eslint loader deprecated (#147) Lint fixes for prior commit The lint fixes for prior commit
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Updates node to 20, webpack to v8 and addresses security updates ``` nvm install 20 nvm use 20 ``` * Updates LTS in `.nvmrc` `npm install webpack@latest --save-dev` - Updates webpack config to address CRA CommonJS bug affecting axios/nock/jest combo facebook/create-react-app#11889 (comment) - Updater syntax change for IgnorePlugin in webpack config - Misc webpack config changes following migration guide: https://webpack.js.org/migrate/5/ - Moves jest config out of package.json into own config - Adds axios to transformIgnoreModules `npm audit fix --force`
Describe the bug
Doing
require('something/something-else')
returns a string — what looks like the path to the bundle file with the lib — instead of returning the actual module that can be used in code. Specifically, the issue was observed withrequire('nanoid/non-secure')
.Using
import * as nano from 'nanoid/non-secure'
works correctly butrequire('nanoid/non-secure')
doesn't. Big part of the issue is that if it's a dependency of your project that's using it with require, you have no control on your app to change the syntax toimport
instead ofrequire
to workaround this issue.Using vanilla Webpack 5 works correctly (outside of react-scripts). Downgrading to react-scripts@^4 also makes it all work correctly. My guess is that it's something in the react-scripts@5 Webpack configuration.
Did you try recovering your dependencies?
No issues with the module tree. Issue is reproducible on a freshly created react-app. It even happens across different yarn setups with versions 1.22.17 and 3.1.0.
Which terms did you search for in User Guide?
"submodules", "require", "require submodule"
Environment
Steps to reproduce
yarn add nanoid
const nano = require('nanoid/non-secure')
nano
; it should be a module with two functions (nanoid
andcustomAlphabet
), but instead is a path that looks like/static/media/index.617173a3029a82877c86.cjs
const { nanoid } = require('nanoid/non-secure')
),nanoid
will be undefined and cause a runtime exception when it's accessed.Expected behavior
require('something/something-else')
should return the module, an object with whatever is exported, not a string path to a file.For example
require('nanoid/non-secure')
, should return{ nanoid: ƒ nanoid(), customAlphabet: ƒ customAlphabet() }
Actual behavior
The
require('something/something-else')
statement returns a string with what looks like the path to the bundle file that contains the lib instead of returning the actual module that can be invoked in code.Using
import * as nano from 'nanoid/non-secure'
works correctly butrequire('nanoid/non-secure')
doesn't. Big part of the issue is that if it's a dependency of your project that's using it with require, you have no control on your app to change the syntax toimport
instead ofrequire
to workaround this issue.Using vanilla Webpack 5 works correctly (no react-scripts involved). Downgrading to react-scripts@^4 also makes it all work correctly. My guess is that it's something in the react-scripts@5 Webpack configuration.
Reproducible demo
I'm attaching 3 things:
yarn create react-app
that shows the issue: react-scripts-5-issue.zipThe text was updated successfully, but these errors were encountered: