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 🙏

© 2026 – Pkg Stats / Ryan Hefner

babel-plugin-transform-line

v0.11.1

Published

Replaces the identifier __line with the source line number

Downloads

162

Readme

babel-plugin-transform-line

npm version License

A simple Babel plugin that replaces the identifier __line with the source line number.

Why Use This?

  • Debugging: Easily track which line of code is executing
  • Logging: Add line numbers to log messages automatically
  • Error Reporting: Include source line information in error messages
  • Zero Runtime Overhead: Line numbers are replaced at build time, not runtime
  • Minimal Setup: Simple to add to your existing Babel configuration

Installation

npm install --save-dev babel-plugin-transform-line

Usage

Add to your Babel configuration

// In your .babelrc or babel.config.js
{
    "plugins": [
        "babel-plugin-transform-line"
    ]
}

In your code

// Your source code
console.log(`Error occurred on line ${__line}`);

// After transformation
console.log(`Error occurred on line ${12}`); // Where 12 is the actual line number

Examples

Simple Usage with Log Statements

const _processData = data => {
    if (!data) {
        console.error(`[Line ${__line}] Invalid data provided`); // [Line 3] Invalid data provided

        return null;
    }

    // Process data
    console.log(`[Line ${__line}] Processing completed`); // [Line 9] Processing completed

    return data;
};

Enhanced Error Objects

import _Error from 'isotropic-error';

try {
    // Some code that might fail
    throw _Error({
        details: {
            line: __line
        },
        message: 'Encountered an error'
    });
} catch (error) {
    console.error(error.details.line); // 7
}

How It Works

This plugin uses Babel's AST visitor pattern to find every occurrence of the __line identifier in your code. When found, it replaces it with a numeric literal representing the source line number where __line appears.

If line information isn't available (which is rare and usually only happens in programmatically generated ASTs), the plugin replaces __line with void 0 (undefined).

Notes

  • The plugin only transforms references to the exact identifier __line. Variables with similar names are not affected.
  • Line numbers refer to the original source code, not the transformed output.
  • This plugin is most helpful during development and debugging. Consider removing it in production builds if line numbers aren't needed.

Use Cases

  • Debugging: Quickly identify which line of code is executing
  • Logging: Add contextual information to log messages
  • Error Reporting: Create more informative error messages
  • Testing: Reference line numbers in test output for easier debugging
  • Documentation: Auto-generate code examples with line numbers

Integration with Other Tools

With Isotropic Logger

import _logger from 'isotropic-logger';

// Usage - use __line at the call site
const _processUser = user => {
    _logger.info({
        line: __line,
        user
    }, 'Processing user');

    if (!user.id) {
        _logger.error({
            line: __line
        }, 'User missing id');

        return false;
    }

  return true;
};

With Mocha and Chai

import _chai from 'isotropic-dev-dependencies/lib/chai.js';
import _mocha from 'isotropic-dev-dependencies/lib/mocha.js';

// Don't actually use this validation function for real
const _validateEmail = email => email !== null && email !== '';

_mocha.describe('Data validation', () => {
    _mocha.it('should validate email addresses', () => {
        const email = '[email protected]',
            result = _validateEmail(email);

        // Include line number in assertion message
        _chai.expect(result, `Email validation failed on line ${__line}`).to.be.true;
    });

    _mocha.it('should handle invalid inputs properly', () => {
        const badInput = null;

        // Easy to see which test assertion failed
        _chai.expect(_validateEmail(badInput), `Null validation failed on line ${__line}`).to.be.false;
        _chai.expect(_validateEmail(''), `Empty string validation failed on line ${__line}`).to.be.false;
    });
});

Important Usage Notes

Remember that __line is replaced with the literal line number where it appears:

// INCORRECT USAGE - You usually don't want to do this!
const LINE = __line; // This becomes const LINE = 2;

// Later in your code
console.log(`This is line ${LINE}`); // Will always print "This is line 2"

// CORRECT USAGE - Use __line directly where you need the line number
console.log(`This is line ${__line}`); // Will print the current line number

Contributing

Please refer to CONTRIBUTING.md for contribution guidelines.

Issues

If you encounter any issues, please file them at https://github.com/ibi-group/babel-plugin-transform-line/issues