TypeScript Compiler API

ts.isExternalModule()

Reports whether a SourceFile carries an external-module indicator. Explicit imports or exports can make a file a module; absence of those constructs does not universally imply script status because compiler module detection and file formats can matter. This does not report runtime module loadability. Baseline: TypeScript 5.8.3; run locally with Node.js, not as a ByteMind CodeRunner exercise.

Formal Syntax & Structure

typescript syntaxtypescript
ts.isExternalModule(file: ts.SourceFile): boolean

Parameters & Attributes

NameTypeDescription
filets.SourceFileSource file from the relevant parse or program.

Working Example

Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");function parse(text) {  return ts.createSourceFile("input.ts", text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS);}assert.equal(ts.isExternalModule(parse("export const x=1;")),true);assert.equal(ts.isExternalModule(parse("const x=1;")),false);assert.equal(ts.isExternalModule(parse("export {};")),true);console.log("exported file and empty export are modules; plain parsed script is not");
// Run: node example.cjs// Expected stdout: exported file and empty export are modules; plain parsed script is not
TypeScript Reference