npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bicycle-codes/debug

v0.6.16

Published

Debug utility

Downloads

2,132

Readme

debug

tests 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

Plus, see the docs generated by typescript.

install

npm i -D @bicycle-codes/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 '@bicycle-codes/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
use multiple modes

Can parse a comma separated list of modes.

A .env file like this:

VITE_DEBUG_MODE="test, staging"

Will log in either "test" or "staging" modes, or if import.meta.env.DEV is true.

vite --mode staging build

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 '@bicycle-codes/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:

import { createDebug } from '@bicycle-codes/debug'
const debug = createDebug('fooo')
const debug2 = createDebug()

debug('debug works')
debug2('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 '@bicycle-codes/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 '@bicycle-codes/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 '@bicycle-codes/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 test