Fix ReferenceError: process is not defined in native browser ESM#3438
Fix ReferenceError: process is not defined in native browser ESM#3438mattgperry merged 3 commits intomainfrom
Conversation
Guard process.env.NODE_ENV access in errors.ts to prevent ReferenceError when importing motion-utils as a native ES module in browsers without bundlers. This adds a typeof check for process before accessing process.env.NODE_ENV. Fixes #3417
Code Review - PR #3438SummaryThis PR fixes a critical bug where importing ✅ Strengths
🔍 Code QualityGood:
Considerations:
🐛 Potential IssuesNone identified. The fix is straightforward and addresses the root cause properly. ⚡ PerformanceNo concerns. The 🔒 SecurityNo concerns. The change is defensive and reduces the risk of runtime errors in browser environments. 🧪 Test CoverageSuggestion: While the fix is simple, it would be valuable to add a test case that validates the module can be imported in a browser-like environment where // packages/motion-utils/src/__tests__/errors.test.ts
describe("errors module", () => {
it("should not throw when process is undefined", () => {
const originalProcess = global.process
try {
// @ts-ignore
delete global.process
// Re-import or re-evaluate the module initialization logic
expect(() => {
// Module import should not throw
}).not.toThrow()
} finally {
global.process = originalProcess
}
})
})However, this is optional since:
🏗️ Build ConfigurationLooking at ✨ RecommendationAPPROVE - This is a solid fix that resolves issue #3417. The implementation is correct, minimal, and follows established patterns for handling Node.js globals in cross-platform JavaScript code. The fix properly enables |
Tests cover warning and invariant functions exported from errors.ts, including verification that error code links are properly formatted.
Code ReviewSummaryThis PR fixes a critical bug where importing motion-utils as a native ES module in browsers (without bundlers) throws ✅ Strengths
🔍 Potential IssuesCritical: Other Packages May Have the Same BugThe fix only addresses
Since Recommendation: Audit all module-level Minor: Test Coverage GapThe test at line 77-88 attempts to verify the process guard but has a limitation: describe("process guard", () => {
it("should handle undefined process gracefully", () => {
// We can't directly test module initialization...
expect(typeof warning).toBe("function")
expect(typeof invariant).toBe("function")
})
})This doesn't actually test the guard because 📋 Code Quality
🎯 Recommendations
Final VerdictApprove with recommendations - The core fix is correct and solves the reported issue. However, the same bug likely exists in other packages ( The fix pattern should be: if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
// dev-only code
}Great work identifying and fixing this ESM compatibility issue! 🎉 |
Code ReviewSummaryThis PR fixes a critical bug where native ES module imports in browsers fail with ✅ Strengths
📝 Minor Observations
🎯 Code Quality Assessment
RecommendationLGTM ✅ - This PR is ready to merge. The fix is correct, minimal, and properly tested. It resolves issue #3417 by ensuring motion-utils can be imported as native ES modules in browsers without bundlers. |
Guard process.env.NODE_ENV access in errors.ts to prevent ReferenceError when importing motion-utils as a native ES module in browsers without bundlers. This adds a typeof check for process before accessing process.env.NODE_ENV.
Fixes #3417