{"id":12089,"library":"strip-literal","title":"Strip Comments and Literals from JavaScript","description":"strip-literal is a JavaScript utility library designed to remove comments and string literals from code while preserving character positions. This is achieved by replacing the stripped content with an equivalent number of spaces, which is crucial for maintaining source map integrity. The library is currently on version 3.1.0 and is actively maintained, with a recent focus on performance improvements. Key differentiators include its reliance on `js-tokens` for robust parsing and its commitment to preserving code structure for tools like linters or code transformers. The project has undergone significant breaking changes, notably transitioning to an ESM-only module in v3.0.0 and switching its internal parsing engine from `acorn` to `js-tokens` in v2.0.0. Releases are fairly regular, addressing bugs and introducing optimizations.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/antfu/strip-literal","tags":["javascript","typescript"],"install":[{"cmd":"npm install strip-literal","lang":"bash","label":"npm"},{"cmd":"yarn add strip-literal","lang":"bash","label":"yarn"},{"cmd":"pnpm add strip-literal","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The default package type became 'module' in v3.0.0, making CommonJS 'require' syntax incompatible in Node.js ESM environments.","wrong":"const { stripLiteral } = require('strip-literal')","symbol":"stripLiteral","correct":"import { stripLiteral } from 'strip-literal'"},{"note":"TypeScript types are included, allowing for type-safe usage and options definition.","symbol":"stripLiteral","correct":"import type { StripLiteralOptions } from 'strip-literal'"},{"note":"The primary export is a named export, `stripLiteral`, not a default export.","wrong":"import stripLiteral from 'strip-literal'","symbol":"stripLiteral","correct":"import * as stripLiteralExports from 'strip-literal'"}],"quickstart":{"code":"import { stripLiteral } from 'strip-literal';\n\n// Example 1: Basic string literal stripping\nconst code1 = `const foo = 'hello world';\\nconsole.log(foo);`;\nconst stripped1 = stripLiteral(code1);\nconsole.log('Stripped 1:\\n', stripped1);\n// Expected: const foo = '           ';\\nconsole.log(foo);\n\n// Example 2: Template literal with interpolation and comments\nconst code2 = `\n  const name = `Alice`; // This is a comment\n  const greeting = `Hello, ${name}!`; /* multi-line\n  comment */\n  console.log(greeting);\n`;\nconst stripped2 = stripLiteral(code2);\nconsole.log('\\nStripped 2:\\n', stripped2);\n/*\nExpected Output:\n\n  const name = `     `;             \n  const greeting = `Hello, ${name}!`;            \n  console.log(greeting);\n*/\n\n// Example 3: Stripping only specific types of literals (e.g., just comments)\n// Note: The library strips all comments and string literals by default.\n// For more granular control, you'd typically need a more advanced parser.\nconst code3 = `// Only comments please\\nconst x = 1; /* another comment */`;\nconst stripped3 = stripLiteral(code3);\nconsole.log('\\nStripped 3:\\n', stripped3);\n// Expected:                  \\nconst x = 1;                   \n","lang":"typescript","description":"Demonstrates how to import and use `stripLiteral` to process JavaScript code, showcasing its behavior with various string and comment types while preserving whitespace for source map compatibility."},"warnings":[{"fix":"Update your import statements from `const { stripLiteral } = require('strip-literal')` to `import { stripLiteral } from 'strip-literal'` and ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`).","message":"Starting with v3.0.0, `strip-literal` is an ES module (ESM) only. CommonJS `require()` calls will no longer work in Node.js environments configured for ESM.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Thoroughly re-test your code transformation pipelines if upgrading from v1.x to v2.x to ensure the new parser behaves as expected for all your input scenarios.","message":"Version 2.0.0 changed the underlying parsing engine from `acorn` to `js-tokens`. While the primary API (`stripLiteral`) remained the same, this internal change could lead to subtle differences in how edge cases or malformed code are handled, potentially causing unexpected outputs for certain inputs.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Be aware that the output code will contain whitespace where comments/literals once were. If you require truly 'removed' content (e.g., for minification), `strip-literal` might need to be part of a larger processing pipeline or a different tool might be more suitable.","message":"The `stripLiteral` function replaces stripped comments and string literals with spaces of the same length, rather than completely removing them. This behavior is intentional to preserve original character positions, which is critical for source map generation and tools relying on accurate file offsets.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement from `const { stripLiteral } = require('strip-literal')` to `import { stripLiteral } from 'strip-literal'`.","cause":"Attempting to use CommonJS `require()` syntax for `strip-literal` in a JavaScript module (ESM) environment after upgrading to v3.0.0.","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Use named import syntax: `import { stripLiteral } from 'strip-literal'`.","cause":"Trying to import `stripLiteral` as a default import (e.g., `import stripLiteral from 'strip-literal'`) when it is exported as a named export.","error":"TypeError: Cannot read properties of undefined (reading 'stripLiteral')"}],"ecosystem":"npm"}