Check if the process is running inside Windows Subsystem for Linux (Bash on Windows)
npx @tessl/cli install tessl/npm-is-wsl@3.1.0is-wsl is a lightweight utility that detects whether the current Node.js process is running inside Windows Subsystem for Linux (WSL). It provides a single boolean value that can be used to implement WSL-specific workarounds for unimplemented or buggy features. The library supports both WSL 1 and WSL 2 environments and handles edge cases like running inside containers.
npm install is-wslimport isWsl from 'is-wsl';For CommonJS environments:
// This package is ES modules only - use dynamic import in CommonJS
(async () => {
const { default: isWsl } = await import('is-wsl');
console.log(isWsl); // boolean value
})();
// Or in async function
async function checkWsl() {
const { default: isWsl } = await import('is-wsl');
return isWsl;
}import isWsl from 'is-wsl';
// Check if running inside WSL
if (isWsl) {
console.log('Running inside Windows Subsystem for Linux');
// Apply WSL-specific workarounds or configurations
} else {
console.log('Not running inside WSL');
// Standard behavior for native Linux/Windows/macOS
}
// Since isWsl is a boolean constant, you can use it directly
import path from 'node:path';
const config = {
useNativeFileWatching: !isWsl, // Disable on WSL due to bugs
pathSeparator: isWsl ? '/' : path.sep,
};Provides a pre-computed boolean value indicating whether the process is running inside WSL.
/**
* Boolean constant indicating whether the current process is running inside WSL
* - Returns true if running inside WSL 1 or WSL 2
* - Returns false on all other platforms (Windows, macOS, native Linux)
* - Returns false when running inside containers within WSL
* - Value is computed once at module load time
*
* Note: When __IS_WSL_TEST__ environment variable is set,
* exports the detection function instead of the computed boolean
*/
declare const isWsl: boolean;
export default isWsl;Detection Logic:
The detection works by examining multiple system indicators:
true on Linux platforms (process.platform === 'linux')os.release() contains "microsoft" (WSL identifier)/proc/version to look for Microsoft signatures in the kernel versionis-inside-container to avoid false positives when running inside Docker/containers within WSLSupported Scenarios:
/**
* Normal export: boolean constant indicating WSL environment
* When __IS_WSL_TEST__ is not set (production usage)
*/
declare const isWsl: boolean;
/**
* Test mode export: detection function for testing
* When __IS_WSL_TEST__ environment variable is set
*/
declare function isWsl(): boolean;
// The actual export depends on the __IS_WSL_TEST__ environment variable
export default isWsl;The library handles all potential errors gracefully:
/proc/version cannot be read, defaults to falsefalse immediately on non-Linux platformsfalse on other platformsis-inside-container@^1.0.0 - Used to detect container environments__IS_WSL_TEST__: When set, exports the detection function instead of the computed value (used internally for testing)When the __IS_WSL_TEST__ environment variable is set, the module exports the detection function instead of the pre-computed boolean value:
// Set environment variable before importing
process.env.__IS_WSL_TEST__ = 'true';
// Import will now export the function, not the boolean
import isWslFunction from 'is-wsl';
// Call the function to get the result
const result = isWslFunction(); // returns boolean
console.log(typeof isWslFunction); // 'function'
console.log(typeof result); // 'boolean'This mode is used internally for testing different system configurations and should not be used in production code.