microsoft/TypeScript · error · Error
tsserverlibrary requires CommonJS; use typescript.js instead
Error message
tsserverlibrary requires CommonJS; use typescript.js instead
What it means
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.
Source
Thrown at Herebyfile.mjs:487
description: "Builds only tsc and tsserver",
dependencies: [tsc, tsserver],
});
export const watchMin = task({
name: "watch-min",
description: "Watches only tsc and tsserver",
hiddenFromTaskList: true,
dependencies: [watchTsc, watchTsserver],
});
// This is technically not enough to make tsserverlibrary loadable in the
// browser, but it's unlikely that anyone has actually been doing that.
const lsslJs = `
if (typeof module !== "undefined" && module.exports) {
module.exports = require("./typescript.js");
}
else {
throw new Error("tsserverlibrary requires CommonJS; use typescript.js instead");
}
`;
const lsslDts = `
import ts = require("./typescript.js");
export = ts;
`;
const lsslDtsInternal = `
import ts = require("./typescript.internal.js");
export = ts;
`;
/**
* @param {string} contents
*/
async function fileContentsWithCopyright(contents) {
return await getCopyrightHeader() + contents.trim().replace(/\r\n/g, "\n") + "\n";
View on GitHub (pinned to b465fdbfe1)
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.
Example fix
// before
import ts from "./tsserverlibrary.js"; // throws in ESM/browser
// after
import ts from "./typescript.js";
// or, under CommonJS:
const ts = require("./tsserverlibrary.js"); Defensive patterns
Strategy: validation
Validate before calling
// Verify the module system before importing tsserverlibrary.
const isCommonJS = typeof module !== "undefined" && !!module.exports;
if (!isCommonJS) {
throw new Error("Load typescript.js instead of tsserverlibrary.js under ESM/browser.");
}
const ts = require("./tsserverlibrary.js"); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- LKG cannot be created when --bundle=false
- Cannot replace the LKG unless all built targets are present
- Diagnostic code ${code} appears more than once.
- Expected ${inputDir} to be a directory
- Expected ${inputFile} to be a file
AI-assisted analysis of microsoft/TypeScript@b465fdbfe1 (2026-08-12).
Data as JSON: /api/errors/cd0d505502d0dfa3.
Report an issue: GitHub.