A Node.js package to decode sourcemaps and map minified positions to their original positions. This tool helps you trace back minified JavaScript code to its original source code using source maps.
- Decode sourcemaps from local files or URLs
- Map minified line and column numbers to original source positions
- Command-line interface for easy usage
- Support for both programmatic and CLI usage
You can install the package in three ways:
-
Global Installation (recommended for frequent use):
npm install -g js-sourcemap-decoder
-
Local Installation (for use in a specific project):
npm install js-sourcemap-decoder
-
Using npx (for one-time use without installation):
npx sourcemap-decode <sourcemap-file> <column> [--line <line>]
The package provides a CLI command sourcemap-decode that can be used in three ways:
-
With global installation:
sourcemap-decode <sourcemap-file> <column> [--line <line>]
-
With local installation:
npx sourcemap-decode <sourcemap-file> <column> [--line <line>]
-
With npx (no installation required):
npx sourcemap-decode <sourcemap-file> <column> [--line <line>]
-
Using default line number (1):
sourcemap-decode bundle.js.map 20
-
Specifying line number:
sourcemap-decode bundle.js.map 20 --line 7617 # or using short flag sourcemap-decode bundle.js.map 20 -l 7617 -
Using a URL:
sourcemap-decode https://example.com/bundle.js.map 20 --line 7617
const { decodeSourceMap } = require('js-sourcemap-decoder');
async function main() {
try {
const result = await decodeSourceMap('bundle.js.map', 20, 7617);
console.log('Original position:', result);
} catch (error) {
console.error('Error:', error.message);
}
}
main();mapFilePath(string): Path to the sourcemap file (URL or local file)column(number): Column number in the minified file (required)line(number): Line number in the minified file (optional, defaults to 1)
Returns a Promise that resolves to an object containing:
source: Original source file pathline: Original line numbercolumn: Original column numbername: Original identifier name (if available)
The package throws specific error types for different scenarios:
InvalidInputError: When input parameters are invalidSourceMapNotFoundError: When the sourcemap file cannot be foundInvalidSourceMapError: When the sourcemap content is invalidNetworkError: When there's an error fetching the sourcemap from a URL
Example error handling:
const {
decodeSourceMap,
InvalidInputError,
SourceMapNotFoundError,
InvalidSourceMapError,
NetworkError
} = require('js-sourcemap-decoder');
async function main() {
try {
const result = await decodeSourceMap('bundle.js.map', 20, 7617);
console.log('Original position:', result);
} catch (error) {
if (error instanceof InvalidInputError) {
console.error('Invalid input:', error.message);
} else if (error instanceof SourceMapNotFoundError) {
console.error('Sourcemap not found:', error.message);
} else if (error instanceof InvalidSourceMapError) {
console.error('Invalid sourcemap:', error.message);
} else if (error instanceof NetworkError) {
console.error('Network error:', error.message);
} else {
console.error('Unknown error:', error.message);
}
}
}
main();-
Clone the repository:
git clone https://github.com/deXterbed/sourcemap-decoder.git cd sourcemap-decoder -
Install dependencies:
npm install
-
Link the package locally:
npm link
The package includes a test suite that can be run using:
npm testThe test suite covers:
- Basic functionality
- Error handling
- Edge cases
- Different input formats
- Node.js v14 or higher
- Internet access if fetching sourcemaps from URLs
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request.
- source-map for the core decoding functionality
- commander.js for the CLI interface
- node-fetch for fetching sourcemaps from URLs