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

console-suppressor

v1.1.1

Published

A console suppressor for your production apps works across all major Javascript and Typescript frameworks.

Downloads

19

Readme

console-suppressor

A console suppressor for your production apps that works across all major JavaScript and TypeScript frameworks. Easily suppress console.log, console.error, and other console statements based on environment or custom conditions.

Table of Contents

Installation

Install console-suppressor in your project via npm or yarn.

Using npm:

npm install console-suppressor

Using yarn:

yarn add console-suppressor

Usage

Basic Usage

To suppress console.log, console.error, and other console statements in your app, import console-suppressor and call the suppress method with your desired options.

import consoleSuppressor from 'console-suppressor';

consoleSuppressor.suppress({
  env: 'production'  // Suppress logs only in production
});

console.log('This will not be shown in production');
console.error('This error will be suppressed in production');

Suppress Logs Based on Custom Condition

You can suppress logs based on any condition by passing a condition function.

import consoleSuppressor from 'console-suppressor';

consoleSuppressor.suppress({
  condition: () => true  // Suppress logs regardless of environment
});

console.log('This log will always be suppressed');

Methods

suppress

This method is the main function to suppress console logs. You can pass either an env string to suppress logs based on the environment or a condition function to suppress logs based on a custom condition.

Parameters:

  • options: An object containing either env or condition.
    • env: A string representing the environment (e.g., 'production', 'development').
    • condition: A function that returns a boolean. If true, logs will be suppressed.

Example: Suppress Logs in Production

consoleSuppressor.suppress({
  env: 'production'  // Suppress logs in production only
});

console.log('This will be suppressed in production');
console.error('This will also be suppressed in production');

Example: Custom Condition for Suppression

consoleSuppressor.suppress({
  condition: () => process.env.SUPPRESS_LOGS === 'true'  // Suppress logs if environment variable is set
});

console.log('This log will be suppressed if SUPPRESS_LOGS is true');

Framework-Specific Usage

React/Next.js

To suppress logs globally in a React or Next.js app, you should call consoleSuppressor.suppress in the root component (App.js or _app.tsx).

React Example:

import React, { useEffect } from 'react';
import consoleSuppressor from 'console-suppressor';

function App() {
  useEffect(() => {
    consoleSuppressor.suppress({
      env: 'production'  // Suppress logs in production
    });
  }, []);

  return <div>My App</div>;
}

export default App;

Next.js Example:

'use client';
import { useEffect } from 'react';
import consoleSuppressor from 'console-suppressor';

function ConsoleSuppressorComponent({
  children
}: Readonly<{
  children: React.ReactNode;
}>) {
  useEffect(() => {
    consoleSuppressor.suppress({
      env: 'production'  // Suppress logs in production
    });
  }, []);

  return children;
}

export default ConsoleSuppressorComponent;
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
};

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={inter.className}>
   <ConsoleSuppressorComponent>{children} </ConsoleSuppressorComponent>

</body>
    </html>
  );
}

Note : Server Components will not be affected , it will work only on client side.

Angular

To apply the suppression globally in an Angular app, import and use consoleSuppressor in your AppComponent or in a service.

Angular Example:

import { Component } from '@angular/core';
import consoleSuppressor from 'console-suppressor';
import { environment } from '../environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor() {
    consoleSuppressor.suppress({
      condition: () => environment.production  // Suppress logs in production
    });
  }
}

Vue.js

In Vue.js, you can suppress logs globally by using consoleSuppressor in the main.js file.

Vue.js Example:

import { createApp } from 'vue';
import App from './App.vue';
import consoleSuppressor from 'console-suppressor';

const app = createApp(App);

consoleSuppressor.suppress({
  env: 'production'  // Suppress logs in production
});

app.mount('#app');

Remix

In Remix, suppress logs globally by adding consoleSuppressor in entry.client.tsx or entry.server.tsx.

Remix Example:

import { RemixBrowser } from '@remix-run/react';
import { hydrate } from 'react-dom';
import consoleSuppressor from 'console-suppressor';

consoleSuppressor.suppress({
  env: 'production'  // Suppress logs in production
});

hydrate(<RemixBrowser />, document);

Vanilla JavaScript

You can also use console-suppressor in any vanilla JavaScript application.

Vanilla JavaScript Example:

import consoleSuppressor from 'console-suppressor';

consoleSuppressor.suppress({
  env: 'production'  // Suppress logs in production
});

console.log('This log will be suppressed in production');

TypeScript Support

console-suppressor is written in TypeScript, and it provides full type support for your TypeScript projects.

TypeScript Example:

import consoleSuppressor from 'console-suppressor';

consoleSuppressor.suppress({
  condition: () => process.env.NODE_ENV === 'production'
});

console.log('This log will be suppressed in production');

Custom Environments

You can also define your own custom environment variables for log suppression by using the condition function. This gives flexibility across all frameworks.

Example:

consoleSuppressor.suppress({
  condition: () => process.env.CUSTOM_ENV === 'production'
});

Contributing

We welcome contributions! Feel free to open an issue or submit a pull request on our GitHub repository.