or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-import-local

Let a globally installed package use a locally installed version of itself if available

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/import-local@3.2.x

To install, run

npx @tessl/cli install tessl/npm-import-local@3.2.0

index.mddocs/

import-local

Let a globally installed package use a locally installed version of itself if available. Useful for CLI tools that want to defer to the user's locally installed version when available, but still work if it's not installed locally.

Package Information

  • Package Name: import-local
  • Package Type: npm
  • Language: JavaScript/TypeScript (Node.js)
  • Installation: npm install import-local

Core Imports

const importLocal = require('import-local');

For ES Modules:

import importLocal from 'import-local';

For TypeScript:

import importLocal from 'import-local';

Basic Usage

const importLocal = require('import-local');

if (importLocal(__filename)) {
    console.log('Using local version of this package');
} else {
    // Code for both global and local version here…
}

For ES Modules:

import importLocal from 'import-local';

if (importLocal(import.meta.url)) {
    console.log('Using local version of this package');
} else {
    // Code for both global and local version here…
}

Capabilities

Local Package Detection

Detects if a locally installed version of the current package is available and imports it if found.

/**
 * Let a globally installed package use a locally installed version of itself if available
 * @param filePath - The absolute file path to the main file of the package (supports __filename for CommonJS or import.meta.url for ES modules)
 * @returns The required local module if found, otherwise false
 */
function importLocal(filePath: string): any | false;

Parameters:

  • filePath (string): The absolute file path to the main file of the package. Can be:
    • __filename when used in CommonJS context
    • import.meta.url when used in ES modules context
    • Any absolute file path to the package's main entry point

Return Value:

  • Returns the required local module if a local version is found and successfully loaded
  • Returns false if no local version is available or if the current execution is already from a local installation

Behavior:

  • Automatically handles both CommonJS (__filename) and ES modules (import.meta.url) contexts
  • Detects whether the current execution is from a global or local installation
  • Uses intelligent path analysis to determine package directories and node_modules structures
  • Cross-platform compatible, handling Windows path case inconsistencies
  • Prevents infinite loops by detecting when already running from local node_modules

Usage Examples:

CommonJS CLI tool:

#!/usr/bin/env node
const importLocal = require('import-local');

if (importLocal(__filename)) {
    // Local version will handle execution
    process.exit(0);
}

// Global version code continues here
console.log('Running global version');

ES Modules entry point:

import importLocal from 'import-local';

if (importLocal(import.meta.url)) {
    // Local version will handle execution
    process.exit(0);
}

// Global version code continues here
console.log('Running global version');

Package main file:

const importLocal = require('import-local');
const path = require('path');

// Check for local version using explicit path
const localModule = importLocal(path.join(__dirname, 'index.js'));
if (localModule) {
    module.exports = localModule;
} else {
    // Export global version functionality
    module.exports = require('./lib/global-implementation');
}