0.6.7 • Published 3 months ago

@nichoth/debug v0.6.7

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

debug

tests Socket Badge module types license

A tiny JavaScript debugging utility that works in Node.js and browsers. Use environment variables to control logging, so there are no ridiculous console log statements in production.

This is based on debug. It's been rewritten to use contemporary JS.

Featuring:

  • Use exports field in package.json to choose node JS or browser version
  • ESM only

install

npm i -D @nichoth/debug

Use this with vite in the browser or in node.


example

browser

This is ergonomic with the vite bundler. This module will look for an env variable prefixed with VITE_:

VITE_DEBUG=fooo

If you initialize this without a namespace, then it checks import.meta.env.DEV:

import Debug from '@nichoth/debug'
const debug = Debug()
debug('debug works')   // check if `import.meta.env.DEV`

a third config option

You can pass in an env variable of VITE_DEBUG_MODE, and then debug will check for that mode in vite.

For example, in the staging environment:
VITE_DEBUG_MODE=staging vite build --mode staging

If you are in production (import.meta.env.PROD) and there is no VITE_DEBUG env var, then this exports a noop, so debug will do nothing, and your bundle will be smaller.

Use a namespace

In your JS code:

import { createDebug } from '@nichoth/debug'
const debug = createDebug('fooo')
debug('debug works')

You would start that script with a VITE_DEBUG=fooo env var to see the log statements.

Don't use a namespace

If you call this without a namespace argument, it will look at the value of import.meta.env.DEV. If you are in DEV mode, then it will log things in a random color:

const debugTwo = createDebug()
// this is only logged if `import.meta.env.DEV` is true
debugTwo('testing debug 2')

setTimeout(() => {
    debug2('log again')
}, 1000)

Screenshot of `debug` in a browser

node JS

Run your script with an env variable, DEBUG.

// in node JS
import createDebug from '@nichoth/debug/node'
const debug = createDebug('fooo')
debug('testing')

Call this with an env var of DEBUG=fooo

DEBUG=fooo node ./test/fixture/node.js

NODE_ENV

If you are in dev mode (process.env.NODE_ENV === 'development'), then this will log things in a random color if you don't initialize it with a namespace --

import createDebug from '@nichoth/debug'
const debug = createDebug()
debug('hello')

Run the script like this:

NODE_ENV=development node ./my-script.js
Configure the environment value

Configure what NODE_ENV value will trigger logging by overriding the shoudlLog function:

// in node only
import Debug from '@nichoth/debug'

Debug.shouldLog = function (NODE_ENV) {
    return NODE_ENV === 'example'
}
const debug = Debug()
// this will log iff we start this like
// NODE_ENV="example" node my-program.js
debug('testing')

develop

browser

Start a vite server and log some things. This uses the example directory.

npm start

node

Run tests:

npm run build-tests
DEBUG=test node ./test/index.js

run examples in node

0.6.7

3 months ago

0.6.6

3 months ago

0.6.3

3 months ago

0.6.2

3 months ago

0.6.5

3 months ago

0.6.4

3 months ago

0.6.1

3 months ago

0.6.0

3 months ago

0.5.0

5 months ago

0.4.1

5 months ago

0.3.2

5 months ago

0.4.0

5 months ago

0.3.1

6 months ago

0.3.0

6 months ago

0.2.9

6 months ago

0.2.8

6 months ago

0.2.7

6 months ago

0.2.6

6 months ago

0.2.5

6 months ago

0.2.4

7 months ago

0.2.3

7 months ago

0.2.2

7 months ago

0.2.1

7 months ago

0.2.0

7 months ago

0.1.5

7 months ago

0.1.4

7 months ago

0.1.3

7 months ago

0.1.2

7 months ago

0.1.1

7 months ago

0.1.0

7 months ago

0.0.17

7 months ago

0.0.16

7 months ago

0.0.15

7 months ago

0.0.14

7 months ago

0.0.13

7 months ago

0.0.12

7 months ago

0.0.11

7 months ago

0.0.10

7 months ago

0.0.9

7 months ago

0.0.8

7 months ago

0.0.7

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago

0.0.0

7 months ago