Create-react-app: Dependencies that use require.ensure for backward compatibility cause warnings

Created on 13 Feb 2019  Â·  7Comments  Â·  Source: facebook/create-react-app

Importing dependencies into an app created with create-react-app that use require.ensure as a fallback option causes the following warning in the console;

./node_modules/pdfjs-dist/build/pdf.js
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

This seems to be because the webpack settings in create-react-app do not allow dependencies to use require.ensure, but it seems that some libraries need to include this as a conditional fallback.

One possible solution might be to remove { parser: { requireEnsure: false } } from the webpack configuration. I don't see much context or discussion about why that rule is important other than that it's not a standard language feature.

I can see the argument from both sides, both why create-react-app would want to warn about this, and why another package might want to include it as a fallback. I don't know the correct solution, but this causes noisy warnings in the console when starting an app that make it more difficult to focus on actionable warnings.

Maybe the correct solution is to allow the suppression of unwanted warnings in create-react-app, but that seems to be a settled issue.

I'm open to any other workarounds or solutions.

needs investigation

Most helpful comment

From what I understand, the code using require.ensure in PDF.js is a fallback for non-webpack environments. In a webpack environment that code is irrelevant, but create-react-app still warns about it because it doesn't know from a static analysis that it's irrelevant.

@gaearon Do you have a specific recommendation for PDF.js contributors @timvandermeij or @Snuffleupagus on what would be the preferred way to support non-webpack users with this fallback? I'm sure this would be of general interest as well, since I've seen quite a few packages where people are reporting this warning.

All 7 comments

Thanks for writing this up!

I'm in the same situation and had to set the CI env variable, for a work-around.
A mechanism for supressing these types of warnings would be a splendid solution.

In longer term we don’t promise continued support for this feature — so it seems to make sense for it to warn. That’s better than if it breaks with no warning in a future release.

Code published to npm ideally shouldn’t be using webpack specific features. Can you explain more about why pdfjs is an exception, and why they use it?

From what I understand, the code using require.ensure in PDF.js is a fallback for non-webpack environments. In a webpack environment that code is irrelevant, but create-react-app still warns about it because it doesn't know from a static analysis that it's irrelevant.

@gaearon Do you have a specific recommendation for PDF.js contributors @timvandermeij or @Snuffleupagus on what would be the preferred way to support non-webpack users with this fallback? I'm sure this would be of general interest as well, since I've seen quite a few packages where people are reporting this warning.

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

I'd really like to see a solution to this issue as it's currently holding us up from using react-pdf without having any alternative.

@akuji1993 same thought 👍

Is there a way to disable warnings for node_modules? Seems kind of harsh to fail a build for warnings in code we don't even control.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

barcher picture barcher  Â·  3Comments

Aranir picture Aranir  Â·  3Comments

onelson picture onelson  Â·  3Comments

alleroux picture alleroux  Â·  3Comments

oltsa picture oltsa  Â·  3Comments