3 [![NPM Version][npm-version-image]][npm-url]
4 [![NPM Downloads][npm-downloads-image]][npm-url]
5 [![Node.js Version][node-image]][node-url]
6 [![Linux Build][travis-image]][travis-url]
7 [![Windows Build][appveyor-image]][appveyor-url]
8 [![Coverage Status][coveralls-image]][coveralls-url]
10 Deprecate all the things
12 > With great modules comes great responsibility; mark things deprecated!
16 This module is installed directly using `npm`:
22 This module can also be bundled with systems like
23 [Browserify](http://browserify.org/) or [webpack](https://webpack.github.io/),
24 though by default this module will alter it's API to no longer display or
29 <!-- eslint-disable no-unused-vars -->
32 var deprecate = require('depd')('my-module')
35 This library allows you to display deprecation messages to your users.
36 This library goes above and beyond with deprecation warnings by
37 introspection of the call stack (but only the bits that it is interested
40 Instead of just warning on the first invocation of a deprecated
41 function and never again, this module will warn on the first invocation
42 of a deprecated function per unique call site, making it ideal to alert
43 users of all deprecated uses across the code base, rather than just
44 whatever happens to execute first.
46 The deprecation warnings from this module also include the file and line
47 information for the call into the module that the deprecated function was
50 **NOTE** this library has a similar interface to the `debug` module, and
51 this module uses the calling file to get the boundary for the call stacks,
52 so you should always create a new `deprecate` object in each file and not
53 within some central file.
57 Create a new deprecate function that uses the given namespace name in the
58 messages and will display the call site prior to the stack entering the
59 file this function was called from. It is highly suggested you use the
60 name of your module as the namespace.
62 ### deprecate(message)
64 Call this function from deprecated code to display a deprecation message.
65 This message will appear once per unique caller site. Caller site is the
66 first call site in the stack in a different file from the caller of this
69 If the message is omitted, a message is generated for you based on the site
70 of the `deprecate()` call and will display the name of the function called,
71 similar to the name displayed in a stack trace.
73 ### deprecate.function(fn, message)
75 Call this function to wrap a given function in a deprecation message on any
76 call to the function. An optional message can be supplied to provide a custom
79 ### deprecate.property(obj, prop, message)
81 Call this function to wrap a given property on object in a deprecation message
82 on any accessing or setting of the property. An optional message can be supplied
83 to provide a custom message.
85 The method must be called on the object where the property belongs (not
86 inherited from the prototype).
88 If the property is a data descriptor, it will be converted to an accessor
89 descriptor in order to display the deprecation message.
91 ### process.on('deprecation', fn)
93 This module will allow easy capturing of deprecation errors by emitting the
94 errors as the type "deprecation" on the global `process`. If there are no
95 listeners for this type, the errors are written to STDERR as normal, but if
96 there are any listeners, nothing will be written to STDERR and instead only
97 emitted. From there, you can write the errors in a different format or to a
100 The error represents the deprecation and is emitted only once with the same
101 rules as writing to STDERR. The error has the following properties:
103 - `message` - This is the message given by the library
104 - `name` - This is always `'DeprecationError'`
105 - `namespace` - This is the namespace the deprecation came from
106 - `stack` - This is the stack of the call to the deprecated thing
108 Example `error.stack` output:
111 DeprecationError: my-cool-module deprecated oldfunction
112 at Object.<anonymous> ([eval]-wrapper:6:22)
113 at Module._compile (module.js:456:26)
114 at evalScript (node.js:532:25)
115 at startup (node.js:80:7)
119 ### process.env.NO_DEPRECATION
121 As a user of modules that are deprecated, the environment variable `NO_DEPRECATION`
122 is provided as a quick solution to silencing deprecation warnings from being
123 output. The format of this is similar to that of `DEBUG`:
126 $ NO_DEPRECATION=my-module,othermod node app.js
129 This will suppress deprecations from being output for "my-module" and "othermod".
130 The value is a list of comma-separated namespaces. To suppress every warning
131 across all namespaces, use the value `*` for a namespace.
133 Providing the argument `--no-deprecation` to the `node` executable will suppress
134 all deprecations (only available in Node.js 0.8 or higher).
136 **NOTE** This will not suppress the deperecations given to any "deprecation"
137 event listeners, just the output to STDERR.
139 ### process.env.TRACE_DEPRECATION
141 As a user of modules that are deprecated, the environment variable `TRACE_DEPRECATION`
142 is provided as a solution to getting more detailed location information in deprecation
143 warnings by including the entire stack trace. The format of this is the same as
147 $ TRACE_DEPRECATION=my-module,othermod node app.js
150 This will include stack traces for deprecations being output for "my-module" and
151 "othermod". The value is a list of comma-separated namespaces. To trace every
152 warning across all namespaces, use the value `*` for a namespace.
154 Providing the argument `--trace-deprecation` to the `node` executable will trace
155 all deprecations (only available in Node.js 0.8 or higher).
157 **NOTE** This will not trace the deperecations silenced by `NO_DEPRECATION`.
161 ![message](files/message.png)
163 When a user calls a function in your library that you mark deprecated, they
164 will see the following written to STDERR (in the given colors, similar colors
165 and layout to the `debug` module):
168 bright cyan bright yellow
172 my-cool-module deprecated oldfunction [eval]-wrapper:6:22
175 namespace | | location of mycoolmod.oldfunction() call
176 | deprecation message
177 the word "deprecated"
180 If the user redirects their STDERR to a file or somewhere that does not support
181 colors, they see (similar layout to the `debug` module):
184 Sun, 15 Jun 2014 05:21:37 GMT my-cool-module deprecated oldfunction at [eval]-wrapper:6:22
187 timestamp of message namespace | | location of mycoolmod.oldfunction() call
188 | deprecation message
189 the word "deprecated"
194 ### Deprecating all calls to a function
196 This will display a deprecated message about "oldfunction" being deprecated
197 from "my-module" on STDERR.
200 var deprecate = require('depd')('my-cool-module')
202 // message automatically derived from function name
203 // Object.oldfunction
204 exports.oldfunction = deprecate.function(function oldfunction () {
205 // all calls to function are deprecated
209 exports.oldfunction = deprecate.function(function () {
210 // all calls to function are deprecated
214 ### Conditionally deprecating a function call
216 This will display a deprecated message about "weirdfunction" being deprecated
217 from "my-module" on STDERR when called with less than 2 arguments.
220 var deprecate = require('depd')('my-cool-module')
222 exports.weirdfunction = function () {
223 if (arguments.length < 2) {
224 // calls with 0 or 1 args are deprecated
225 deprecate('weirdfunction args < 2')
230 When calling `deprecate` as a function, the warning is counted per call site
231 within your own module, so you can display different deprecations depending
232 on different situations and the users will still get all the warnings:
235 var deprecate = require('depd')('my-cool-module')
237 exports.weirdfunction = function () {
238 if (arguments.length < 2) {
239 // calls with 0 or 1 args are deprecated
240 deprecate('weirdfunction args < 2')
241 } else if (typeof arguments[0] !== 'string') {
242 // calls with non-string first argument are deprecated
243 deprecate('weirdfunction non-string first arg')
248 ### Deprecating property access
250 This will display a deprecated message about "oldprop" being deprecated
251 from "my-module" on STDERR when accessed. A deprecation will be displayed
252 when setting the value and when getting the value.
255 var deprecate = require('depd')('my-cool-module')
257 exports.oldprop = 'something'
259 // message automatically derives from property name
260 deprecate.property(exports, 'oldprop')
263 deprecate.property(exports, 'oldprop', 'oldprop >= 0.10')
270 [npm-version-image]: https://img.shields.io/npm/v/depd.svg
271 [npm-downloads-image]: https://img.shields.io/npm/dm/depd.svg
272 [npm-url]: https://npmjs.org/package/depd
273 [travis-image]: https://img.shields.io/travis/dougwilson/nodejs-depd/master.svg?label=linux
274 [travis-url]: https://travis-ci.org/dougwilson/nodejs-depd
275 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/nodejs-depd/master.svg?label=windows
276 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/nodejs-depd
277 [coveralls-image]: https://img.shields.io/coveralls/dougwilson/nodejs-depd/master.svg
278 [coveralls-url]: https://coveralls.io/r/dougwilson/nodejs-depd?branch=master
279 [node-image]: https://img.shields.io/node/v/depd.svg
280 [node-url]: https://nodejs.org/en/download/