{"id":11930,"library":"rehype-remove-script-type-javascript","title":"Remove Script Type JavaScript Rehype Plugin","description":"This `rehype` plugin, `rehype-remove-script-type-javascript`, is designed to optimize HTML documents by automatically removing redundant `type` and `language` attributes from `<script>` elements that inherently contain JavaScript. It intelligently identifies and targets scripts with types like `text/javascript` or `language` attributes, leaving module scripts (`type=\"module\"`) and other non-JavaScript script types untouched. The package's current stable version is `4.0.1`. It operates within the `unified` ecosystem, processing `hast` syntax trees. While `rehype-remove-script-type-javascript` has its own versioning, it is part of the `rehype-minify` monorepo, which recently underwent a significant `7.0.0` major release, introducing ecosystem-wide changes such as a strict Node.js 16+ requirement and exclusive ESM distribution. This plugin helps improve page load performance by reducing the byte size of HTML, as these attributes are often unnecessary in modern browsers where JavaScript is the default script type. It offers a focused, declarative approach to HTML optimization within a `unified` pipeline.","status":"active","version":"4.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/rehypejs/rehype-minify#main","tags":["javascript","html","mangle","minify","plugin","rehype","rehype-plugin","remove","typescript"],"install":[{"cmd":"npm install rehype-remove-script-type-javascript","lang":"bash","label":"npm"},{"cmd":"yarn add rehype-remove-script-type-javascript","lang":"bash","label":"yarn"},{"cmd":"pnpm add rehype-remove-script-type-javascript","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core processor for building content pipelines.","package":"unified","optional":false},{"reason":"Required to parse HTML input into a HAST (HTML Abstract Syntax Tree) for processing.","package":"rehype-parse","optional":false},{"reason":"Required to serialize the processed HAST back into an HTML string.","package":"rehype-stringify","optional":false},{"reason":"Used for representing virtual files in the unified ecosystem, common for input/output.","package":"vfile","optional":false}],"imports":[{"note":"This package is ESM-only since v4.0.0, aligning with the unified ecosystem's transition to ESM and Node.js 16+ requirement.","wrong":"const rehypeRemoveScriptTypeJavaScript = require('rehype-remove-script-type-javascript');","symbol":"rehypeRemoveScriptTypeJavaScript","correct":"import rehypeRemoveScriptTypeJavaScript from 'rehype-remove-script-type-javascript';"}],"quickstart":{"code":"import rehypeParse from 'rehype-parse';\nimport rehypeRemoveScriptTypeJavaScript from 'rehype-remove-script-type-javascript';\nimport rehypeStringify from 'rehype-stringify';\nimport {unified} from 'unified';\nimport {VFile} from 'vfile';\n\nasync function processExampleHtml() {\n  const htmlInput = `<!doctype html><html><head></head><body>\n    <script type=\"text/javascript\">console.log('Legacy JS');</script>\n    <script language=\"javascript\">alert('Another legacy JS');</script>\n    <script type=\"module\">import './modern.js';</script>\n    <script type=\"application/json\">{\"data\": \"json-script\"}</script>\n    <script src=\"external.js\" type=\"text/javascript\"></script>\n  </body></html>`;\n\n  const inputVFile = new VFile({ path: 'input.html', value: htmlInput });\n\n  const outputVFile = await unified()\n    .use(rehypeParse, { fragment: false }) // Process as a full document\n    .use(rehypeRemoveScriptTypeJavaScript)\n    .use(rehypeStringify)\n    .process(inputVFile);\n\n  console.log('Original HTML:\\n', htmlInput);\n  console.log('\\nProcessed HTML:\\n', String(outputVFile));\n}\n\nprocessExampleHtml();","lang":"typescript","description":"Demonstrates how to integrate `rehype-remove-script-type-javascript` into a `unified` processing pipeline to remove redundant script type attributes from an HTML file."},"warnings":[{"fix":"Upgrade Node.js to version 16 or higher.","message":"The `rehype` ecosystem, including this plugin, transitioned to requiring Node.js 16 or newer with the `rehype-minify` monorepo v7.0.0 release. Older Node.js versions will encounter compatibility issues.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Use ES module syntax (e.g., `import rehypeRemoveScriptTypeJavaScript from 'rehype-remove-script-type-javascript';`) and ensure your project is configured for ESM (e.g., `'type': 'module'` in `package.json` or `.mjs` file extension).","message":"This package is now distributed as an ES Module (ESM) only, affecting how it's imported in Node.js applications. CommonJS `require()` statements will fail.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always use `rehype-sanitize` as part of your unified pipeline when processing untrusted or user-generated HTML content.","message":"Improper handling of HTML with `rehype` can lead to Cross-Site Scripting (XSS) vulnerabilities. While this specific plugin removes attributes, the broader `rehype` processing can be unsafe if untrusted HTML is processed without sanitization.","severity":"gotcha","affected_versions":"*"},{"fix":"Update all related `unified` ecosystem packages (e.g., `unified`, `rehype-parse`, `rehype-stringify`) to their latest compatible major versions.","message":"The `rehype-minify` monorepo v7.0.0 release involved significant updates to underlying `unified` and `@types/hast` dependencies, requiring users to also update their `unified` core and related utilities to maintain compatibility.","severity":"breaking","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const rehypeRemoveScriptTypeJavaScript = require('rehype-remove-script-type-javascript');` to `import rehypeRemoveScriptTypeJavaScript from 'rehype-remove-script-type-javascript';`. Ensure your Node.js environment supports ESM (e.g., `type: 'module'` in `package.json` or `.mjs` file extension).","cause":"Attempting to use CommonJS `require()` to import an ESM-only package.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/rehype-remove-script-type-javascript/index.js from ... not supported."},{"fix":"Ensure your code `await`s the `process` call (e.g., `await unified()...process(...)`). Verify Node.js is v16+ and `unified` package is updated to a compatible version (e.g., v11+).","cause":"This usually indicates that the `process` call isn't `await`ed, or the Node.js version is too old for the async API, or `unified` itself is an old version not compatible with the async API.","error":"TypeError: unified(...).use(...).process is not a function"},{"fix":"Add `\"type\": \"module\"` to your project's `package.json` or rename the file to have a `.mjs` extension. Ensure Node.js v16+ is used.","cause":"Using `import` syntax in a file that Node.js interprets as a CommonJS module (e.g., a `.js` file without `type: \"module\"` in `package.json` in a directory).","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm"}