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.

Webpack should have a way to ignore `require` calls

See original GitHub issue

Bug report

What is the current behavior?

Webpack tries to handle all require() calls, even those not meant for browser usage. This makes it almost impossible to have a module that works in both Node.js and the browser, but has some added capabilities in Node.js.

Note that this is about authoring reusable npm packages, not end-user apps. I don’t have the ability to set any Webpack config like the externals option since I’m making packages other devs will consume.

This is the kind of awfulness we have to do because of Webpack: https://github.com/sindresorhus/ow/blob/d62a06c192b892d504887f0b97fdc842e8cbf862/source/utils/node/require.ts We have wasted countless of hours on this.

Prior discussion that went mostly ignored by the Webpack team: https://github.com/webpack/webpack/issues/196#issuecomment-354900072

What is the expected behavior?

I expected to be able to annotate the code to make Webpack ignore certain require() calls.

For example with a comment:

const require('chalk'); // webpack-ignore

I know for certain that this require() call will not be used in the browser, so I should be able to have it in my source code without Webpack printing warnings to users or erroring out.

I need this as a module maintainer (not user).

Telling users to use the externals config is out of the questions, as my package might be a dependency many levels down.

Some more examples of where I have to use awful code to work around Webpack:

Other relevant information: webpack version: 4.29.0 (applies to any version though) Node.js version: 10.15.0 Operating System: macOS 10.14.3 Additional tools:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:90
  • Comments:32 (15 by maintainers)

github_iconTop GitHub Comments

20reactions
sindresorhuscommented, Mar 5, 2019

However, I don’t think that webpack is to blame for this either. Dynamic imports is something no bundler is able to handle in a good way.

This is not about dynamic imports.

If webpack did not print out a warning for dynamic requires, people might get “Could not find module xyz” on runtime which is worst imo.

No, they would not get “Could not find module xyz” at runtime because the require call would be behind a conditional that is never hit in the browser. I’m asking for a way to tell Webpack to ignore a require() call because I know best. When automatic behavior fails, we should be able to override it.

I see the magic comment as a last resort because it’s webpack specific. I would like to have something that works with all bundlers.

The browser field in package.json is not a standard either. We could come up with a universally recognized magic ignore comment.

The browser field in the package.json is something that all bundlers respect.

Do you have a source for that assertion? Can you point me to the Webpack docs about the browser field?

So you would have two entries in your module, one that is intended for browsers and one that is intended for node. Then you would require all node-only dependencies in your main entry.

You’re asking me to massively refactor my codebase to support Webpack. It’s not as easy as just using the browser field. It would mean creating entry points that can easily be swapped out. It’s not always that simple.

All I want is to make Webpack ignore a require call that is not used in the browser…

11reactions
techheadcommented, Jul 19, 2020

For anyone else encountering this issue and need a workaround, this is probably the best one: https://twitter.com/kamilogorek/status/1102272038411137025

This is what I ended up going with for doing a conditional import in the NodeJS environment.

const nodeVer = typeof process !== "undefined" && process.versions?.node;
const nodeRequire = nodeVer
  ? typeof __webpack_require__ === "function"
    ? __non_webpack_require__
    : require
  : undefined;

Webpack will optimize away the bare require in this case so that you don’t end up with the ugly warning below (and extra code injected).

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I make webpack skip a require - node.js
If you store the path in a variable then IgnorePlugin will not work. Though you still could do:
Read more >
IgnorePlugin - webpack
IgnorePlugin prevents the generation of modules for import or require calls matching the regular expressions or filter functions: ...
Read more >
Module Methods - webpack
This section covers all methods available in code compiled with webpack. When using webpack to bundle your application, you can pick from a...
Read more >
TerserWebpackPlugin | webpack
To begin, you'll need to install terser-webpack-plugin : ... then you need to setup explicitly number of CPUs to avoid Error: Call retries...
Read more >
babel-loader - webpack
Within your webpack configuration object, you'll need to add the ... You can instead require the Babel runtime as a separate module to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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