Course contentsShow
← Web Development
Lesson 1418 of 2,98225. Build Tools and BundlersFree lesson

Side Effects in Package.json

Configuring the sideEffects field to help bundlers tree shake third-party packages.

Side Effects in Package.json

You'll learn how to tell bundlers which files in a package have side effects, enabling more aggressive tree shaking.

The Concept

When you learned about tree shaking, you saw how bundlers remove unused code. But there's a problem: bundlers must be cautious. Some JavaScript files execute code just by being imported—even if you don't use any exports. These are called side effects.

For example, a file might:

  • Modify global objects (window.myGlobal = true)
  • Register polyfills
  • Apply CSS imports
  • Set up event listeners

If a bundler incorrectly removes a file with side effects, your app breaks. So by default, bundlers assume every file might have side effects and keep them all.

The sideEffects Field

You can help bundlers make smarter decisions by adding a sideEffects field to your package.json:

{
  "name": "my-library",
  "sideEffects": false
}

This tells bundlers: "None of my files have side effects. If something isn't imported, remove it completely."

For packages that do have some side effects, specify them explicitly:

{
  "name": "my-library",
  "sideEffects": ["./src/polyfills.js", "*.css"]
}

Now bundlers know: "These specific files must stay, but everything else can be tree shaken aggressively."

Why This Matters

When you install third-party packages, the sideEffects field is your bundler's guide. A well-configured package can be tree shaken down to only the functions you actually use, dramatically reducing bundle size. Without it, the bundler plays it safe and includes more code than necessary.

Key Takeaway: The sideEffects field in package.json tells bundlers which files are safe to remove during tree shaking, enabling more aggressive dead code elimination.