Skip to content

Commit 99cd853

Browse files
MichaelGHSegclaude
andauthored
Remove node-fetch dependency from @segment/analytics-node (#1348)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3f1f585 commit 99cd853

File tree

7 files changed

+11
-23
lines changed

7 files changed

+11
-23
lines changed

.changeset/remove-node-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-node': major
3+
---
4+
5+
Remove node-fetch dependency in favor of native globalThis.fetch (available since Node 18, package requires >= 20)

packages/node/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"@segment/analytics-generic-utils": "1.2.0",
4141
"buffer": "^6.0.3",
4242
"jose": "^5.1.0",
43-
"node-fetch": "^2.6.7",
4443
"tslib": "^2.4.1"
4544
},
4645
"devDependencies": {

packages/node/src/__tests__/typedef-tests.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
GroupTraits,
99
HTTPClient,
1010
FetchHTTPClient,
11-
HTTPFetchFn,
1211
HTTPClientRequest,
1312
} from '../'
1413

@@ -90,10 +89,8 @@ export default {
9089
console.log({} as UserTraits)
9190
},
9291

93-
'HTTPFetchFn should be compatible with standard fetch and node-fetch interface, as well as functions':
92+
'HTTPFetchFn should be compatible with standard fetch interface, as well as functions':
9493
() => {
95-
const fetch: HTTPFetchFn = require('node-fetch')
96-
new Analytics({ writeKey: 'foo', httpClient: fetch })
9794
new Analytics({ writeKey: 'foo', httpClient: globalThis.fetch })
9895
},
9996

packages/node/src/app/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface AnalyticsSettings {
4343
/**
4444
* Supply a default http client implementation (such as one supporting proxy).
4545
* Accepts either an HTTPClient instance or a fetch function.
46-
* Default: an HTTP client that uses globalThis.fetch, with node-fetch as a fallback.
46+
* Default: an HTTP client that uses globalThis.fetch.
4747
*/
4848
httpClient?: HTTPFetchFn | HTTPClient
4949
/**

packages/node/src/lib/fetch.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
import type { HTTPFetchFn } from './http-client'
22

3-
export const fetch: HTTPFetchFn = async (...args) => {
4-
if (globalThis.fetch) {
5-
return globalThis.fetch(...args)
6-
}
7-
// This guard causes is important, as it causes dead-code elimination to be enabled inside this block.
8-
// @ts-ignore
9-
else if (typeof EdgeRuntime !== 'string') {
10-
return (await import('node-fetch')).default(...args)
11-
} else {
12-
throw new Error(
13-
'Invariant: an edge runtime that does not support fetch should not exist'
14-
)
15-
}
3+
export const fetch: HTTPFetchFn = (...args) => {
4+
return globalThis.fetch(...args)
165
}

packages/node/src/lib/http-client.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { abortSignalAfterTimeout } from './abort'
22
import { fetch as defaultFetch } from './fetch'
33

44
/**
5-
* This interface is meant to be compatible with different fetch implementations (node and browser).
6-
* Using the ambient fetch type is not possible because the AbortSignal type is not compatible with node-fetch.
5+
* This interface is meant to be compatible with different fetch implementations.
76
* @link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
87
*/
98
export interface HTTPFetchFn {
@@ -18,7 +17,7 @@ export interface HTTPFetchRequest {
1817
headers: Record<string, string>
1918
body: string
2019
method: HTTPClientRequest['method']
21-
signal: any // AbortSignal type does not play nicely with node-fetch
20+
signal: any
2221
}
2322

2423
/**

yarn.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4459,7 +4459,6 @@ __metadata:
44594459
axios: ^1.6.2
44604460
buffer: ^6.0.3
44614461
jose: ^5.1.0
4462-
node-fetch: ^2.6.7
44634462
tslib: ^2.4.1
44644463
languageName: unknown
44654464
linkType: soft

0 commit comments

Comments
 (0)