Registry / serialization / esniff

esniff

JSON →
library2.0.1jsnpmunverified

esniff is a specialized, low-footprint JavaScript source code parser designed for use cases that require syntax-aware analysis without needing to construct a full Abstract Syntax Tree (AST). It excels at tasks like finding specific function calls or property accesses by navigating the code based on syntax rules, which is more robust than regular expression matching. The current stable version is 2.0.1, released in February 2024. The package maintains a relatively active release cadence, with several minor and patch updates and a major breaking change (v2.0.0) in early 2024. Its key differentiator is its focus on efficiency and targeted code fragment detection over comprehensive AST generation, making it suitable for performance-critical analysis where a full AST would be overkill.

npm install esniff
INSTALL
IMPORT
SIG · ESNIFF
E
esniff
serializationjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

esniff
import esniff from 'esniff';
const esniff = require('esniff');
While `require` syntax is shown in examples, modern Node.js applications should use ESM `import`. esniff primarily works with a functional `executor` pattern in v2.x.
accessedProperties
import accessedProperties from 'esniff/utils/accessed-properties';
import accessedProperties from 'esniff/accessed-properties';
Since v2.0.0, utility modules like `accessedProperties` and `function` were moved under the `utils/` subdirectory.
findFunctionCalls
import findFunctionCalls from 'esniff/utils/function';
import findFunctionCalls from 'esniff/function';
The `function` utility, used to find function invocations, was moved to `esniff/utils/function` in v2.0.0. The import is for a default export, often aliased for clarity.

Demonstrates how to use `esniff` to parse all `require()` calls within a given code string, leveraging the `trigger` event and `accessor` methods.

import esniff from 'esniff'; const parseRequires = (code) => { return esniff(code, (emitter) => { emitter.on('trigger:r', (accessor) => { if (accessor.previousToken === '.') return; // Avoid 'foo.require' if (!accessor.skipCodePart('require')) return; accessor.skipWhitespace(); accessor.collectScope(); // Collects content within parentheses }); }); }; const codeExample = ` const dep1 = require('module-a'); var dep2 = global.require('module-b'); function foo() { return require('module-c'); } console.log('No require here'); `; const results = parseRequires(codeExample); console.log(results); /* Expected output for 'require('foo/bar')' example in README: [ { type: 'scope', point: 17, column: 17, line: 1, raw: "'foo/bar'" } ] Note: Actual output will vary based on the full codeExample above. */
Debug
Known issues
breakingThe main `esniff` interface changed significantly in v2.0.0. It moved from `esniff(code, trigger, callback)` to `esniff(code, executor)`, where `executor` receives an `emitter` for controlling the parsing process.
fix
Rewrite calls to `esniff` to use the new `executor` function signature, interacting with the provided `emitter` and `accessor` object. Refer to the v2.0.0 documentation for the updated API.
affects: >=2.0.0
breakingIn v2.0.0, the resolution of property and variable names now adheres to ES2015+ language rules, rather than the older ES5 rules. This may cause changes in how certain identifiers are recognized or handled, potentially leading to different analysis results for older or non-standard code.
fix
Review existing code analysis logic to ensure compatibility with modern ECMAScript identifier rules. No direct code fix for `esniff` itself, but the interpretation of results might change.
affects: >=2.0.0
breakingSeveral utility modules, such as `ensure-string-literal`, `is-string-literal`, `is-var-name-valid`, `accessed-properties`, and `function`, were relocated to a `utils/` subdirectory in v2.0.0. Direct imports to their previous paths will now fail.
fix
Update import paths for all affected utility modules to include the `utils/` prefix. For example, `require('esniff/accessed-properties')` becomes `require('esniff/utils/accessed-properties')`.
affects: >=2.0.0
gotchaesniff assumes the input code is syntactically correct and will not report syntax errors. Providing malformed code may lead to unexpected or nonsensical results.
fix
Ensure that code passed to `esniff` is syntactically valid. Consider pre-validating code with a linter or a more robust parser if syntax error detection is required.
affects: >=1.0.0
gotchaesniff has a known limitation where a division operator (`/`) directly following an object literal (e.g., `x = { foo: 'bar' } / 14`) may be incorrectly interpreted as the start of a regular expression, producing incorrect results.
fix
Be aware of this specific edge case. If such code patterns are expected, manual pre-processing or alternative parsing might be necessary for those specific code fragments. Most real-world code does not exhibit this pattern.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: esniff is not a function or esniff is not callable
Attempting to use the pre-v2.0.0 API signature with a v2.x version of esniff.
fix
Update your `esniff` calls to match the v2.0.0 `esniff(code, executor)` signature. The second argument should be a function that receives an `emitter`.
Error: Cannot find module 'esniff/accessed-properties'
Attempting to import a utility module from its old path after the v2.0.0 breaking change.
fix
Change the import path to include the `utils/` subdirectory. For example, `require('esniff/accessed-properties')` should be `require('esniff/utils/accessed-properties')`.
ReferenceError: require is not defined
Using CommonJS `require()` syntax in an ESM module context without proper configuration or transpilation.
fix
For modern Node.js environments configured for ESM (e.g., `"type": "module"` in `package.json`), use `import esniff from 'esniff';` instead of `const esniff = require('esniff');`. If both ESM and CJS are needed, ensure dual-package authoring or use a bundler that handles interoperability.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
esniff — npm install esniff · libregistry