Check if a path is inside another path
npx @tessl/cli install tessl/npm-is-path-inside@4.0.0is-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.
npm install is-path-insideimport 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().
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');
//=> trueDetermines 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 pathparentPath (string): The path that should contain the child pathReturns: boolean - Returns true if childPath is inside parentPath, false otherwise
Behavior:
process.cwd() to make them absolutepath module for cross-platform compatibilityfalse for identical paths)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)