fix: Resolve require() incompatibility in ESM and UMD builds - #248
Conversation
Three environments broke when keep-alive agents were introduced using
require('http') and require('https'):
1. Native ESM (ReferenceError: require is not defined)
- Added typeof require === 'undefined' guard in createKeepAliveAgent()
- Falls back to false (pre-1.5.0 behaviour) in ESM environments
2. Turbopack/webpack (expression is too dynamic)
- Replaced require(moduleName) variable arg with static string literals
- require('http') / require('https') are statically analysable by bundlers
3. UMD webpack build (Can't resolve 'http')
- Added externals for http and https in config/webpack.config.js
- UMD wrapper resolves them at runtime in Node; isNodeEnvironment guard
prevents execution in browser where they are not available
Tests added:
- test/esm-compat.mjs: native ESM integration test (run after build)
- test/contentstack-core.node-agent.spec.ts: Jest wrapper spawning esm-compat.mjs
- test/esm-exports.spec.ts: static analysis asserting no dynamic require(variable)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
🔒 Security Scan Results
⏱️ SLA Breach Summary
✅ BUILD PASSED - All security checks passed |
🔒 Security Scan Results
⏱️ SLA Breach Summary
✅ BUILD PASSED - All security checks passed |
There was a problem hiding this comment.
Pull request overview
This PR updates @contentstack/core’s HTTP keep-alive agent setup to avoid require() failures in native ESM and to make bundlers (webpack/Turbopack) able to statically analyze require('http'|'https'), while also adjusting the UMD build to avoid resolving Node built-ins during bundling.
Changes:
- Refactors keep-alive agent creation to use
typeof require !== 'undefined'and staticrequire('http')/require('https')calls. - Adds package-level browser mappings for
http/httpsand webpack UMD externals for Node built-ins. - Adds/extends tests to validate ESM build safety and a native ESM regression check for issue #246.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/lib/contentstack-core.ts |
Guards keep-alive agent creation for native ESM and replaces dynamic require(moduleName) with static literals. |
config/webpack.config.js |
Externalizes http/https for the UMD build to prevent bundler resolution of Node built-ins. |
package.json |
Adds browser mappings for Node built-ins and bumps version. |
package-lock.json |
Updates locked package version metadata. |
test/esm-exports.spec.ts |
Adds assertions to ensure the built ESM output does not contain dynamic require(variable) calls. |
test/esm-compat.mjs |
Adds a native ESM integration script validating no crash and agent fallback behavior. |
test/contentstack-core.node-agent.spec.ts |
Adds a Jest test that runs the native ESM integration script as a child process. |
.talismanrc |
Adds an ignore entry for the new ESM compat test script. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
🔒 Security Scan Results
⏱️ SLA Breach Summary
✅ BUILD PASSED - All security checks passed |
… fix Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
🔒 Security Scan Results
⏱️ SLA Breach Summary
✅ BUILD PASSED - All security checks passed |
fix: Resolve require() incompatibility in ESM and UMD builds
Summary
ReferenceError: require is not defined) — addedtypeof require === 'undefined'guard increateKeepAliveAgent(), falls back tofalsein ESM environmentsexpression is too dynamic) — replacedrequire(moduleName)variable arg with static string literals so bundlers can statically analyse the callCan't resolve 'http') — addedexternalsforhttp/httpsinconfig/webpack.config.js; UMD wrapper resolves them at runtime in Node,isNodeEnvironmentguard prevents execution in browserTest plan
npm run build— all four targets (cjs, esm, umd, types) compile cleannode test/esm-compat.mjs— native ESM agents fall back tofalse, no crashnpm test— existing and new Jest specs passRelated
Closes #246
🤖 Generated with Claude Code