{"id":12741,"library":"linq","title":"LINQ for JavaScript","description":"Linq.js is a comprehensive JavaScript implementation of the .NET Language Integrated Query (LINQ) library, offering a fluent API for querying and manipulating data collections. It includes all the original .NET LINQ methods along with several additions, enabling powerful data transformations and filtering across various iterable objects like arrays, Maps, and Sets. The current stable version is 4.0.3, with a release cadence that has provided incremental updates for improved iteration protocol support and enhanced TypeScript definitions. A key differentiator is its transition to a pure ES module structure in version 4.0.0, making it fully compatible with modern JavaScript environments such as Node.js (v14.13.1 and later), TypeScript projects, Deno, and contemporary web browsers. For projects requiring CommonJS modules or targeting older environments, version 3 of the library remains available.","status":"active","version":"4.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/mihaifm/linq","tags":["javascript","linq","typescript"],"install":[{"cmd":"npm install linq","lang":"bash","label":"npm"},{"cmd":"yarn add linq","lang":"bash","label":"yarn"},{"cmd":"pnpm add linq","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library is an ES module since v4.0.0. CommonJS 'require' syntax will fail; use 'linq@3' for CJS projects.","wrong":"const Enumerable = require('linq')","symbol":"Enumerable","correct":"import Enumerable from 'linq'"},{"note":"IEnumerable is a type available via the default 'Enumerable' import, not a named export itself.","wrong":"import { IEnumerable } from 'linq'","symbol":"IEnumerable<T>","correct":"import Enumerable from 'linq'; type MyCollection = Enumerable.IEnumerable<number>;"},{"note":"This import pattern is only correct for linq@3 and earlier versions. linq@4+ is ESM-only.","wrong":"import Enumerable from 'linq'","symbol":"Enumerable (CommonJS)","correct":"const Enumerable = require('linq')"}],"quickstart":{"code":"// For Node.js (v14.13.1+) with \"type\": \"module\" in package.json\nimport Enumerable from 'linq';\n\n// Example 1: Basic filtering and transformation\nlet result1 = Enumerable.range(1, 10)\n    .where(i => i % 3 === 0)\n    .select(i => i * 10)\n    .toArray();\nconsole.log('Filtered and multiplied:', result1); // [ 30, 60, 90 ]\n\n// Example 2: Working with objects and iterating over Map/Set\nconst data = new Map([\n    ['apple', 10],\n    ['banana', 20],\n    ['cherry', 30]\n]);\nlet result2 = Enumerable.from(data)\n    .where(([key, value]) => value > 15)\n    .select(([key, value]) => ({ fruit: key, quantity: value * 2 }))\n    .toArray();\nconsole.log('Querying Map:', result2); // [ { fruit: 'banana', quantity: 40 }, { fruit: 'cherry', quantity: 60 } ]\n\n// Example 3: Finding the first element meeting a condition\nlet firstBigRadius = Enumerable.toInfinity(1)\n    .where(r => r * r * Math.PI > 10000)\n    .first();\nconsole.log('First radius for area > 10000:', firstBigRadius);","lang":"typescript","description":"Demonstrates basic LINQ operations including filtering, mapping, and querying iterable objects like Maps, highlighting its fluent API."},"warnings":[{"fix":"For new projects, ensure your Node.js environment is v14.13.1+ and your `package.json` includes `'type': 'module'`. For existing CommonJS projects, continue using `npm install linq@3`.","message":"Version 4.0.0 fully transitioned to an ES module (ESM) architecture, deprecating CommonJS (CJS) support for the current major release. This means `require()` syntax is no longer supported with `linq@4` and newer.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Add `'type': 'module'` to your `package.json` file to enable ES module resolution. Alternatively, rename your `.js` files to `.mjs` or use `linq@3` for CommonJS.","message":"Attempting to use `linq@4` or newer in a Node.js project without configuring it as an ES module will result in module resolution errors. This is typically indicated by a `SyntaxError: Cannot use import statement outside a module`.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Configure your `tsconfig.json` with `{'compilerOptions': {'target': 'ES2020', 'moduleResolution': 'node'}}` to ensure types are correctly resolved and compiled.","message":"The library ships with TypeScript definitions, but correct usage requires specific `tsconfig.json` compiler options, particularly `target` set to `ES2020` or higher and `moduleResolution` to `node`.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Avoid using `Enumerable.Utils.extendTo` to modify built-in prototypes. Prefer direct instantiation with `Enumerable.from()` or method chaining on `Enumerable` directly to avoid global object mutation.","message":"Version 3.2.3 introduced `Enumerable.Utils.recallFrom` which implies that `Enumerable.Utils.extendTo` (for prototype pollution) should be used with extreme caution, as direct prototype extension is generally discouraged in modern JavaScript.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Add `'type': 'module'` to your `package.json` or switch to `linq@3` and use `const Enumerable = require('linq')`.","cause":"Attempting to use ES module `import` syntax for `linq@4+` in a CommonJS Node.js project.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"For `linq@4+`, use `import Enumerable from 'linq'` in an ES module project. For CommonJS, use `linq@3` and `const Enumerable = require('linq')`.","cause":"Using `require('linq')` with `linq@4+` in a Node.js ES module project, or in an environment where the global `Enumerable` is not exposed (e.g., modern browser without `<script type='module'>`).","error":"ReferenceError: Enumerable is not defined"},{"fix":"Do not attempt to instantiate `Enumerable` with `new`. Call its static methods directly, e.g., `Enumerable.range(1, 10)` or `Enumerable.from(myArray)`.","cause":"The `Enumerable` object is a singleton or a static class-like object, not a constructor. It is meant to be used with static methods like `Enumerable.range()` or `Enumerable.from()`.","error":"TypeError: 'Enumerable' is not a constructor"},{"fix":"Import `Enumerable` as a default import and then use `Enumerable.IEnumerable<T>` for type annotation: `import Enumerable from 'linq'; type MyType = Enumerable.IEnumerable<string>;`. Ensure `tsconfig.json` includes `{'compilerOptions': {'target': 'ES2020', 'moduleResolution': 'node'}}`.","cause":"`IEnumerable` is a type available via the default `Enumerable` import, not a named export itself, or `tsconfig.json` is not configured correctly.","error":"TS2305: Module '\"linq\"' has no exported member 'IEnumerable'."}],"ecosystem":"npm"}