Cross-platform implementation of Node.js util.deprecate() function with browser support
npx @tessl/cli install tessl/npm-util-deprecate@1.0.0The Node.js util.deprecate() function with browser support. Provides a cross-platform implementation for marking functions as deprecated with configurable warning behavior.
npm install util-deprecateconst deprecate = require('util-deprecate');For ES modules (using a bundler like webpack or browserify):
import deprecate from 'util-deprecate';const deprecate = require('util-deprecate');
// Mark a function as deprecated
const oldFunction = function() {
return 'This function works but is deprecated';
};
// Create a deprecated version with a custom warning message
const deprecatedFunction = deprecate(
oldFunction,
'oldFunction() is deprecated, use newFunction() instead'
);
// Export the deprecated function
exports.oldFunction = deprecatedFunction;
// When users call the function, they'll see the warning once:
// deprecatedFunction(); // Logs: "oldFunction() is deprecated, use newFunction() instead"
// deprecatedFunction(); // No warning on subsequent callsutil-deprecate uses a dual-implementation approach to provide cross-platform compatibility:
util.deprecate() function from Node.js core, ensuring full compatibility with Node.js deprecation system and environment variablesbrowser field when using bundlers like browserify or webpackMark functions as deprecated with customizable warning messages and behavior control through environment configuration.
/**
* Mark that a method should not be used.
* Returns a modified function which warns once by default.
*
* @param {Function} fn - the function to deprecate
* @param {String} msg - the string to print to the console when fn is invoked
* @returns {Function} a new "deprecated" version of fn
*/
function deprecate(fn, msg);Parameters:
fn (Function): The function to deprecatemsg (String): The warning message to display when the deprecated function is invokedReturns:
Function: A new "deprecated" wrapper version of the original function that:
Warning Behavior:
The deprecation warning behavior can be controlled through different mechanisms depending on the environment:
Node.js Environment:
util.deprecate() functionBrowser Environment:
localStorage.noDeprecation = 'true': Disables all deprecation warningslocalStorage.throwDeprecation = 'true': Throws an Error instead of showing a warninglocalStorage.traceDeprecation = 'true': Uses console.trace() to show stack trace instead of console.warn()Usage Examples:
const deprecate = require('util-deprecate');
// Simple deprecation
const oldApi = deprecate(
function(data) { return data.toUpperCase(); },
'oldApi() is deprecated, use newApi() instead'
);
// Deprecate a class method
class MyClass {
constructor() {
this.newMethod = this.newMethod.bind(this);
this.oldMethod = deprecate(
this.oldMethod.bind(this),
'MyClass.oldMethod() is deprecated, use MyClass.newMethod() instead'
);
}
newMethod(data) {
return data.toLowerCase();
}
oldMethod(data) {
return this.newMethod(data);
}
}
// Configure warning behavior in browser
if (typeof localStorage !== 'undefined') {
// Disable all deprecation warnings
localStorage.noDeprecation = 'true';
// Or throw errors instead of warnings
localStorage.throwDeprecation = 'true';
// Or show stack traces
localStorage.traceDeprecation = 'true';
}The package provides different implementations based on the runtime environment:
Node.js (node.js):
util.deprecate function from Node.js coreBrowser (browser.js):
console.warn(), console.trace(), or thrown ErrorThe appropriate implementation is automatically selected based on the environment when using a bundler like browserify or webpack that respects the browser field in package.json.