axios.CancelToken deprecation warning causing CI build failures
0 reputation · 11 May 2020, 10:33 UTC
0 reputation · 11 May 2020, 10:33 UTC
Since axios v0.27.0, the legacy CancelToken API emits a console deprecation warning when used. In repeatable development environments where CI pipelines enforce strict no‑console rules, this warning can cause builds to fail even though the cancellation logic remains functional.
The goal is to adopt the native AbortController approach across both Node.js and browser contexts while preserving the ability to cancel requests reliably and without triggering CI‑blocking warnings.
What migration patterns minimize disruption to existing codebases? Can a targeted ESLint rule or build‑time filter suppress only the axios deprecation warning without disabling all console output? Are there version‑specific shims that allow conditional use of CancelToken for older axios versions while defaulting to AbortController in newer releases?
To eliminate the deprecation warning and stop CI build failures, replace axios.CancelToken.source() with the native AbortController API. Pass the controller.signal into the axios request configuration object. This removes the trigger for the console warning while maintaining request cancellation functionality.
AbortSignal via the signal config option.CancelToken API began emitting a deprecation warning in axios v0.27.0.AbortController is natively available in all modern browsers and Node.js v15.0.0+.The CI failure occurs because axios performs an internal check when CancelToken is instantiated. If the project uses a strict "no-console" policy or treats console.warn as a build error, the deprecation message triggers a pipeline failure even though the code remains logically functional.
To minimize disruption, implement a wrapper that abstracts the cancellation logic. This allows you to update the underlying mechanism without changing every call site in the codebase.
// cancellation-provider.js
export function createRequestController() {
const controller = new AbortController();
return {
signal: controller.signal,
cancel: () => controller.abort(),
};
}
Implementation:
const { signal, cancel } = createRequestController();axios.get('/endpoint', { signal });cancel() to abort the request..catch() block handles the error using axios.isCancel(error) to distinguish between network failures and intentional cancellations.Build-time filtering: Since ESLint's no-console rule typically flags the call to console.warn (which happens inside the axios library, not your code), ESLint cannot suppress this. To filter the output in CI, you must wrap the global console.warn in your entry point:
const originalWarn = console.warn;
console.warn = (...args) => {
if (typeof args[0] === 'string' && args[0].includes('CancelToken is deprecated')) return;
originalWarn(...args);
};
Version-specific shims: If your environment supports Node.js versions older than v15.0.0, you must install a polyfill (e.g., npm install abort-controller) and import it at the top of your application to ensure AbortController is defined globally.
To determine if a polyfill is required, please provide the Node.js version used in your CI environment (node -v). If the version is below 15.0.0, the native AbortController approach will throw a ReferenceError.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.