babel-plugin-js-logger

raw JSON →
1.0.17 verified Sat Apr 25 auth: no javascript

Babel plugin that automatically inserts a logger declaration (via js-logger) at the top of every transpiled JavaScript file, enabling consistent logging across backend and frontend projects. Current stable version: 1.0.17. Maintained as of early 2021, with infrequent updates. Key differentiators: automatic logger instantiation per file, configurable logger name formatting (project/level/separator/extensions), and support for both Babel standalone and Webpack. Alternative to manual logger setup in each file. Note: requires js-logger as a peer dependency and Babel as a build tool.

error Module not found: Error: Can't resolve 'js-logger'
cause js-logger is not installed or not in node_modules.
fix
Install js-logger as a dependency: npm install js-logger --save
error Error: Cannot find module 'babel-plugin-js-logger'
cause The plugin is not installed or not listed in .babelrc plugins correctly.
fix
Install the plugin: npm install babel-plugin-js-logger --save-dev, then add 'js-logger' to .babelrc plugins array.
error TypeError: require(...).useDefaults is not a function
cause js-logger is not initialized properly.
fix
Call require('js-logger').useDefaults() before using the generated logger variables.
gotcha With ES modules (import), the plugin's generated logger declaration may execute after imports due to hoisting, causing initialization order issues.
fix Use a separate init file imported first, e.g., import './logger-init.js'; (see README).
gotcha The plugin automatically inserts a require() for js-logger even if it's not used, potentially causing tree-shaking to fail.
fix Consider using manual logger setup if tree-shaking is critical.
gotcha The format option 'level' with negative values counts from the project root, which may include unwanted directories if project root detection fails.
fix Test with your project structure and adjust level or extensions accordingly.
gotcha The plugin does not support TypeScript natively; TypeScript files must be handled by Babel with appropriate presets.
fix Use @babel/preset-typescript and ensure the plugin runs after it.
npm install babel-plugin-js-logger
yarn add babel-plugin-js-logger
pnpm add babel-plugin-js-logger

Installs and configures the plugin, initializes js-logger, and demonstrates automatic logger insertion per file.

// Install: npm install js-logger --save && npm install babel-plugin-js-logger --save-dev

// .babelrc
{
  "plugins": ["js-logger"]
}

// In your main entry point (e.g., main.js)
require('js-logger').useDefaults(); // initialize js-logger

// All other files will automatically have a logger variable
// Example: some/path/file.js
// The plugin inserts: const logger = require('js-logger').get('project:some:path:file');

// Use logger
logger.info('Hello from file!');