DEV Community

Sebastian Nitu
Sebastian Nitu

Posted on

Why do bundlers import module fields but don't transpile by default?

  1. By default bundlers resolve a packages import from the module field over the main in package.json (webpack and rollup, for example) which points to a projects ES6 module entry.

  2. By default bundlers ignore transformations on anything from node_modules.

Aren't these two behaviors contradictory?

There are solutions to this:

  • Change the mainFields option to import main before module.
  • Change your exclude option to transpile specific package(s).
  • Use an es-check and only transpile packages that need it.

But yeah, this has always confused me. Am I missing something? Why do they do this?

Top comments (2)

Collapse
 
jordienr profile image
Jordi Enric

Well, sometimes you want to bundle but don't want to transpile. The best thing about webpack is that you can configure it however you like.

Collapse
 
sebnitu profile image
Sebastian Nitu

Yeah, totally. I love webpack too and have found ways to configure a bundle and resolve the issue in both webpack and rollup. I guess my general question is why does it act this way by default? It feels very counter-intuitive and while I was searching for solutions I noticed A LOT of similar posts of people trying to resolve that same issue.