Registry / serialization / hast-util-is-javascript

hast-util-is-javascript

JSON →
library3.0.1jsnpmunverified

`hast-util-is-javascript` is a utility package within the unified/rehype ecosystem designed to determine if a given `hast` (HTML Abstract Syntax Tree) node represents a JavaScript script. It specifically checks `<script>` elements, evaluating their `type` and `language` attributes to ascertain if they contain or reference JavaScript, or if these attributes are absent, implying JavaScript by default. The current stable version is `3.0.1`. As part of the `rehypejs` project, it follows a coordinated release schedule, often releasing minor or patch updates across multiple packages, with major version increments for significant breaking changes, such as the recent `7.0.0` ecosystem release which introduced ESM-only modules and a Node.js 16+ requirement. Its primary differentiator is its seamless integration with other `hast` manipulation tools and its strict adherence to WHATWG HTML parsing standards.

npm install hast-util-is-javascript
INSTALL
IMPORT
SIG · HAST-UTIL-IS-JAVAS
H
hast-util-is-javascript
serializationjavascriptv3.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.

isJavaScript
import { isJavaScript } from 'hast-util-is-javascript'
const isJavaScript = require('hast-util-is-javascript')
This package is ESM-only since v3; CommonJS `require` is not supported. Additionally, `isJavaScript` is a named export, not a default export.

Demonstrates how to use `isJavaScript` to check various `hast` script nodes for JavaScript content based on their `type` or `language` attributes, or when these attributes are implicitly JavaScript.

import { h } from 'hastscript'; import { isJavaScript } from 'hast-util-is-javascript'; // Create a script element without type/language, which defaults to JS const jsScript = h('script'); console.log("Is plain script JS?", isJavaScript(jsScript)); // Expected output: Is plain script JS? true // Create a script element with explicit JS type const jsScriptWithType = h('script', { type: 'text/ecmascript' }); console.log("Is script with text/ecmascript type JS?", isJavaScript(jsScriptWithType)); // Expected output: Is script with text/ecmascript type JS? true // Create a script element with explicit JS language const jsScriptWithLanguage = h('script', { language: 'ecmascript' }); console.log("Is script with ecmascript language JS?", isJavaScript(jsScriptWithLanguage)); // Expected output: Is script with ecmascript language JS? true // Create a script element with a non-JS type const nonJsScript = h('script', { type: 'text/fooscript' }); console.log("Is script with text/fooscript type JS?", isJavaScript(nonJsScript)); // Expected output: Is script with text/fooscript type JS? false // Create a div element (not a script) const divElement = h('div'); console.log("Is a div element JS?", isJavaScript(divElement)); // Expected output: Is a div element JS? false
Debug
Known issues
breakingThis package is now pure ES module (ESM) and no longer provides CommonJS (CJS) exports. Direct `require()` calls will fail.
fix
Migrate your project to use ES module imports (`import ... from '...'`) instead of CommonJS `require()` statements.
affects: >=3.0.0
breakingNode.js 16 or greater is now required for this package. Older Node.js versions are no longer supported.
fix
Upgrade your Node.js environment to version 16 or newer. Check the unified collective's compatibility guidelines for specific version requirements.
affects: >=3.0.0
gotchaWorking with HTML ASTs like `hast` can expose applications to Cross-Site Scripting (XSS) vulnerabilities if not handled carefully, especially when injecting user-supplied content.
fix
Always sanitize your `hast` trees, especially when dealing with user-generated or untrusted content. Use `rehype-sanitize` to make the tree safe before rendering to HTML.
affects: *
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to import `hast-util-is-javascript` using CommonJS `require()` syntax in an environment that enforces ESM.
fix
Change your import statement from `const isJavaScript = require('hast-util-is-javascript')` to `import { isJavaScript } from 'hast-util-is-javascript'`.
TypeError: Cannot read properties of undefined (reading 'isJavaScript') or TypeError: isJavaScript is not a function
Attempting to use `import isJavaScript from 'hast-util-is-javascript'` which assumes a default export, but `isJavaScript` is a named export.
fix
Ensure you are using a named import: `import { isJavaScript } from 'hast-util-is-javascript'`.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
hast-util-is-javascript — npm install hast-util-is-javascript · libregistry