Registry / web-framework / is-expression

is-expression

JSON →
library4.0.0jsnpmunverified

The `is-expression` package provides a utility function for synchronously validating if a given string is a syntactically correct JavaScript expression. It is currently at version 4.0.0 and appears to have an infrequent release cadence, with the latest major update focusing on parser upgrades. The library uses Acorn under the hood to perform its parsing, allowing for detailed error reporting when the `throw` option is enabled. Key differentiators include its simplicity, direct integration with Acorn options, and the ability to toggle strict mode or allow line comments, making it suitable for environments like templating engines (e.g., Pug) or dynamic code evaluation where robust expression validation is crucial. It focuses purely on syntax, not semantic validity or runtime behavior.

npm install is-expression
INSTALL
IMPORT
SIG · IS-EXPRESSION
I
is-expression
web-frameworkjavascriptv4.0.0
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.

isExpression
import { isExpression } from 'is-expression'
import isExpression from 'is-expression'
The primary function is a named export, not a default export.
isExpression
const isExpression = require('is-expression')
CommonJS require style is fully supported and shown in documentation examples.
isExpression
import * as isExpressionModule from 'is-expression'; isExpressionModule.isExpression('foo')
While functional, direct named import is generally preferred for clarity.

This quickstart demonstrates basic expression validation using `isExpression` and how to leverage `throw`, `strict`, and `lineComment` options to control parsing behavior and error handling.

import { isExpression } from 'is-expression'; // Basic validation console.log(isExpression('myVar')); // => true console.log(isExpression('var')); // => false (keyword, not expression) console.log(isExpression('["an", "array"].indexOf("index")')); // => true // Using options: throw on error try { isExpression('const foo', { throw: true }); // 'const' is a declaration, not an expression } catch (error) { console.error('Caught expected error:', error.message); // SyntaxError: Unexpected token 'const' (1:0) } // Using options: strict mode console.log(isExpression('public')); // => true (in non-strict mode) console.log(isExpression('public', { strict: true })); // => false (in strict mode, 'public' is a reserved keyword) // Using options: allowing comments console.log(isExpression('abc // my comment')); // => false (comments not allowed by default) console.log(isExpression('abc // my comment', { lineComment: true })); // => true
Debug
Known issues
breakingVersion 4.0.0 upgraded the underlying Acorn parser from 4.0.2 to 7.1.1. This change significantly updates the default parsing mode to ES2019, meaning many constructs previously considered invalid expressions will now parse successfully as valid ES2019 expressions.
fix
Review existing uses of `is-expression` after upgrading to v4.0.0. If you relied on certain syntax being invalid for older JavaScript versions, you may need to introduce custom parsing options via `options` or implement additional validation logic.
affects: >=4.0.0
gotchaBy default, `isExpression` does not allow line comments within the string being validated. An expression like `a + b // comment` will be considered invalid.
fix
If line comments are expected and should be treated as part of a valid expression, you must explicitly pass `{ lineComment: true }` in the options object to `isExpression`.
affects: >=1.0.0
gotchaThe `isExpression` function defaults to non-strict mode parsing. This can lead to certain keywords or syntax (e.g., `public`) being accepted as valid expressions, which might be invalid in strict-mode contexts.
fix
For stricter validation consistent with modern JavaScript modules or strict-mode code, always provide `{ strict: true }` in the options object to `isExpression`.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token (1:0) at Parser.pp.raise (acorn/dist/acorn.js:XXX:YY) ...
Attempting to validate a string that is not a valid JavaScript expression or contains syntax not allowed by the default Acorn parsing options.
fix
Ensure the input string is a true JavaScript expression. If you require specific parsing allowances (e.g., comments, non-standard syntax), pass appropriate `options` like `{ lineComment: true }` or Acorn-specific parser options. If the error is about a keyword like 'var' or 'const', remember these are statements/declarations, not expressions.
ReferenceError: isExpression is not defined
The `isExpression` function was not correctly imported or required.
fix
For CommonJS, use `const isExpression = require('is-expression');`. For ES Modules, use `import { isExpression } from 'is-expression';`. Ensure your environment supports the chosen module system.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
is-expression — npm install is-expression · libregistry