{"record":{"id":"cd0d505502d0dfa3","repo":"microsoft/TypeScript","slug":"tsserverlibrary-requires-commonjs-use-typescript","errorCode":null,"errorMessage":"tsserverlibrary requires CommonJS; use typescript.js instead","messagePattern":"tsserverlibrary requires CommonJS; use typescript\\.js instead","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Herebyfile.mjs","lineNumber":487,"sourceCode":"    description: \"Builds only tsc and tsserver\",\r\n    dependencies: [tsc, tsserver],\r\n});\r\n\r\nexport const watchMin = task({\r\n    name: \"watch-min\",\r\n    description: \"Watches only tsc and tsserver\",\r\n    hiddenFromTaskList: true,\r\n    dependencies: [watchTsc, watchTsserver],\r\n});\r\n\r\n// This is technically not enough to make tsserverlibrary loadable in the\r\n// browser, but it's unlikely that anyone has actually been doing that.\r\nconst lsslJs = `\r\nif (typeof module !== \"undefined\" && module.exports) {\r\n    module.exports = require(\"./typescript.js\");\r\n}\r\nelse {\r\n    throw new Error(\"tsserverlibrary requires CommonJS; use typescript.js instead\");\r\n}\r\n`;\r\n\r\nconst lsslDts = `\r\nimport ts = require(\"./typescript.js\");\r\nexport = ts;\r\n`;\r\n\r\nconst lsslDtsInternal = `\r\nimport ts = require(\"./typescript.internal.js\");\r\nexport = ts;\r\n`;\r\n\r\n/**\r\n * @param {string} contents\r\n */\r\nasync function fileContentsWithCopyright(contents) {\r\n    return await getCopyrightHeader() + contents.trim().replace(/\\r\\n/g, \"\\n\") + \"\\n\";\r","sourceCodeStart":469,"sourceCodeEnd":505,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/Herebyfile.mjs#L469-L505","documentation":"The TypeScript build generates a tsserverlibrary.js shim that only works under CommonJS: it checks `typeof module !== \"undefined\" && module.exports` and otherwise throws. tsserverlibrary is intentionally Node/CommonJS-only (it re-exports ./typescript.js); the wrapper blocks ESM and browser loads because the full compiler bundle is neither ESM-safe nor browser-safe.","triggerScenarios":"Importing the built 'built/local/tsserverlibrary.js' (or the published 'tsserverlibrary.js') via an ES module `import`, through a bundler emitting ESM, or in a browser where the global `module` object is undefined.","commonSituations":"Editor plugins or tooling that static-import tsserverlibrary; Webpack/Rollup configs with `output.module: true` or `type: \"module\"`; consuming the published package from a `.mjs` file.","solutions":["Import 'typescript.js' instead of 'tsserverlibrary.js' (or use the package root 'typescript') in ESM/browser code.","If you need tsserverlibrary specifically, load it via `require()` under CommonJS, or configure your bundler to treat it as CJS (module.type === 'commonjs').","For browser-based language-service work, use a browser-packaged build rather than tsserverlibrary."],"exampleFix":"// before\nimport ts from \"./tsserverlibrary.js\"; // throws in ESM/browser\n\n// after\nimport ts from \"./typescript.js\";\n// or, under CommonJS:\nconst ts = require(\"./tsserverlibrary.js\");","handlingStrategy":"validation","validationCode":"// Verify the module system before importing tsserverlibrary.\nconst isCommonJS = typeof module !== \"undefined\" && !!module.exports;\nif (!isCommonJS) {\n  throw new Error(\"Load typescript.js instead of tsserverlibrary.js under ESM/browser.\");\n}\nconst ts = require(\"./tsserverlibrary.js\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer importing the package root ('typescript') or typescript.js; reserve tsserverlibrary for Node CommonJS editor hosts.","Mark tsserverlibrary.js as CommonJS in your bundler config (module.type = 'commonjs') so it loads via require.","In browser targets, never reach for tsserverlibrary — use a browser-packaged build."],"tags":["build","commonjs","esm","browser","tsserverlibrary"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}