or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-is-path-inside

Check if a path is inside another path

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-path-inside@4.0.x

To install, run

npx @tessl/cli install tessl/npm-is-path-inside@4.0.0

index.mddocs/

is-path-inside

is-path-inside provides a utility function to check if one file system path is inside another path. It performs path manipulation using Node.js's built-in path module to calculate relative paths and determine containment relationships without actually accessing the file system or resolving symlinks.

Package Information

  • Package Name: is-path-inside
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Requirements: Node.js >=12
  • Installation: npm install is-path-inside

Core Imports

import isPathInside from 'is-path-inside';

Note: This package is ES module only and requires Node.js 12 or later. It cannot be imported using require().

Basic Usage

import isPathInside from 'is-path-inside';

// Check if path is inside another path
isPathInside('a/b/c', 'a/b');
//=> true

isPathInside('a/b/c', 'x/y');
//=> false

// A path is not inside itself
isPathInside('a/b/c', 'a/b/c');
//=> false

// Works with absolute paths
isPathInside('/Users/sindresorhus/dev/unicorn', '/Users/sindresorhus');
//=> true

Capabilities

Path Containment Check

Determines whether one path is contained within another path using relative path calculation.

/**
 * Check if a path is inside another path.
 * Note that relative paths are resolved against process.cwd() to make them absolute.
 * 
 * Important: This package is meant for use with path manipulation. It does not 
 * check if the paths exist nor does it resolve symlinks. You should not use this 
 * as a security mechanism to guard against access to certain places on the file system.
 * 
 * @param childPath - The path that should be inside parentPath
 * @param parentPath - The path that should contain childPath
 * @returns true if childPath is inside parentPath, false otherwise
 */
function isPathInside(childPath: string, parentPath: string): boolean;

Parameters:

  • childPath (string): The path that should be inside the parent path
  • parentPath (string): The path that should contain the child path

Returns: boolean - Returns true if childPath is inside parentPath, false otherwise

Behavior:

  • Relative paths are automatically resolved against process.cwd() to make them absolute
  • Uses Node.js built-in path module for cross-platform compatibility
  • Works on both POSIX (Unix/Linux/macOS) and Windows file systems
  • A path is never considered inside itself (returns false for identical paths)
  • Does not check if paths actually exist on the file system
  • Does not resolve symbolic links
  • Should not be used as a security mechanism for file system access control

Usage Examples:

import isPathInside from 'is-path-inside';

// Basic containment check
console.log(isPathInside('a/b/c', 'a/b')); // true
console.log(isPathInside('a/b/c', 'x/y')); // false

// Self-containment check
console.log(isPathInside('a/b/c', 'a/b/c')); // false

// Absolute paths
console.log(isPathInside('/home/user/documents/file.txt', '/home/user')); // true
console.log(isPathInside('/home/user', '/home/user/documents')); // false

// Relative paths (resolved against process.cwd())
console.log(isPathInside('./subfolder/file.txt', '.')); // true
console.log(isPathInside('../sibling', '.')); // false

// Cross-platform path separators are handled automatically
console.log(isPathInside('a\\b\\c', 'a\\b')); // true (on Windows)
console.log(isPathInside('a/b/c', 'a/b')); // true (on Unix-like systems)