Use package entry points for conditional exports between cjs/esm - #1249
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR modernizes the package entry point configuration by implementing Node.js Conditional Exports to resolve import/require discrepancies between CommonJS and ES modules. The change replaces reliance on legacy fields with a structured approach that provides different module formats based on the runtime environment and import method.
- Introduces conditional exports configuration in package.json for Node.js >= 12.7.0
- Creates a CommonJS wrapper file to bridge ES modules for require() usage
- Updates the main field to point to the new CommonJS wrapper
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| package.json | Adds exports field with conditional entry points and updates main field |
| src/server/rollbar.cjs | New CommonJS wrapper that re-exports the default export from the ES module |
| // This file provides CommonJS compatibility for Node.js users | ||
| // who require() the rollbar package | ||
|
|
||
| module.exports = require('./rollbar.js').default; No newline at end of file |
There was a problem hiding this comment.
This line could fail if './rollbar.js' doesn't have a default export or if the module loading fails. Consider adding error handling or validation to provide clearer error messages when the ES module cannot be loaded or lacks a default export.
Suggested change
| module.exports = require('./rollbar.js').default; | |
| try { | |
| const rollbarModule = require('./rollbar.js'); | |
| if (!rollbarModule || !rollbarModule.default) { | |
| throw new Error("The './rollbar.js' module does not have a default export."); | |
| } | |
| module.exports = rollbarModule.default; | |
| } catch (error) { | |
| throw new Error(`Failed to load './rollbar.js': ${error.message}`); | |
| } |
waltjones
reviewed
Jul 20, 2025
waltjones
approved these changes
Jul 20, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of the change
This PR fixes more issues around
importandrequirediscrepancies by introducing a modern way to define package entry points, called Conditional Exports which were introduced 6 years ago in Node 12.7.0.Conditional Exports replace the legacy
main,browserandmodulefields providing fine-grained control over what files can be imported from our package and how.This change was prompted by the
universal-nodeexample which wouldn't be able torequire('rollbar')without suffixing.defaultto it. This is because node looked into ourpackage.jsonand resolved to use ourmainfield which points to an es module.Note
Before Conditional Exports, packages had to expose main and rely on hacks like pkg.module (non-standard) for ESM builds. Conditional exports made dual packages clean and official.
See the official Proposal for Bare Module Specifier Resolution in node.js
The introduced changes override
main,browserandmodulefields for all nodes >= 12.7.0, and any node < 12 will fallback to the older fields. I'm keeping everything to maximize compatibility.Important
With this change I can confirm that all examples that integrate rollbar through their
package.jsonfile are 100% working.Type of change
Related issues