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

nodejs-better-console

v1.0.4

Published

Console messages will now include timestamp, file path and line number (allowing automatic traversal to location in file), viewing Objects of all depth levels, filtering by text and console method(log, debug, warn, error).

Downloads

14

Readme

NodeJs Maintained Status Ask Me Anything Issues Count Size

Description

Testing a node.js script is considerably different than testing javascript in a browser. Debugging node.js can be fun and easy. This package provides additional functionality for debugging node.js via console messages. Only one function call is necessary at the entrance JavaScript file (ie. index.js) to override console functionality throughout the script.

Features

  • Timestamp for console message
  • File path, file name, and line number for console message call
  • Automatic traversal to location of console.log command via CTRL + LEFT CLICK.
    • Caveat: This has only been confirmed to work with vscode, where the node.js script is executed in the terminal provided by vscode. (Open vscode terminal: CTRL + ` )
  • Viewing Objects of all depth levels
  • Filtering log messages by any of the following method types: ['debug', 'log', 'warn', 'error']
  • Filtering console output.
    • Works exactly as you would expect if you used the browser filter functionality. Supported: JS Primitive Datatypes, Arrays, Objects, Functions (Function Name), Classes (Class Name)

Javascript Support

Fully Supported: CJS(CommonJS) and ESM(ECMAScript modules, ES6)

Usage

Basic and Intermediate Install

npm i nodejs-better-console

Basic

import { overrideConsole } from 'nodejs-better-console';
// CJS import example
// const {overrideConsole} = require('nodejs-better-console');

overrideConsole();

console.log('log test');
console.debug('debug test');
console.warn('warn test');
console.error('error test');

alt text

Intermediate

Additional configuration parameters can be passed in to unlock the full potential of this package (Filter Features) and/or limit other features

Default config if nothing is passed in

const config = {
    context: './',
    obj_depth: null,
    show_timestamp: true,
    filter_console_method: ['debug', 'log', 'warn', 'error'],
    filter_console_output: '',
};

Usage of config

Example 1 (Console Output Filter)

const config = {
    filter_console_output: 'cool',
};
overrideConsole(config);

console.log('amazing');
console.log('this is so cool');

alt text

Example 2 (Console Method Filter)

const config = {
    filter_console_method: ['debug'],
};
overrideConsole(config);

console.log('this is a log message');
console.debug('this is a debug message');

alt text

Example 3 (Console Output Filter and Method Filter)

const config = {
    filter_console_method: ['debug'],
    filter_console_output: 'cool',
};
overrideConsole(config);

console.log('this is a log message');
console.debug('this is a debug message');
console.debug('this is a cooler debug message');

alt text

Example 4 (Context)

const config = {
    context: './src',
};
overrideConsole(config);

console.log('this is a log message');

alt text

Example 5 (Object Depth)

const config = {
    obj_depth: 3,
};
const obj = {
    a: {
        b: {
            c: {
                d: {
                    e: 5,
                },
            },
        },
    },
};
overrideConsole(config);

console.log(obj);

alt text

Example 6 (Timestamp)

const config = {
    timestamp: false,
};
overrideConsole(config);

console.log('this is a log message');

alt text

Advanced

Install

npm i -D nodejs-better-console npm i dotenv

Example

.env

NODE_ENV=development

index.js

import dotenv from 'dotenv';
// CJS import example
// const dotenv = require('dotenv')
dotenv.config();

if (process.env.NODE_ENV !== 'development') {
    console.log('test console message NOT from development');
}

if (process.env.NODE_ENV === 'development') {
    const { overrideConsole } = await import(
        'nodejs-better-console'
    );
    // CJS import example
    // const {overrideConsole} = require('nodejs-better-console')

    overrideConsole();

    console.log('test console message from development');
}

alt text

Recommendation and Warning

This package is a javascript console override, this means that the override itself is not isolated to this package only. For example, if this package was to be imported and used within your node.js script, if another package attempts a console message [i.e., console.log('some message')], this package's version of the console would be used to output the message.

This package should ONLY be used for development purposes and not for production.

See Advanced Usage for fix