no-top-level-await
UnreleasedConfiguration
Disallow top-level await in published modules.
Rule details
ES modules that use top-level await cannot be loaded with require().
This rule helps libraries support both import styles by reporting top-level
await, for await...of, and await using in published files. Async functions
and their bodies are allowed.
A file is checked when its nearest package.json includes it in files, or
has no files field, and .npmignore does not exclude it. .gitignore is used
when both files and .npmignore are absent. The package's main entry remains
published. Files outside a package are ignored.
Examples of incorrect code in a published file:
Example of correct code:
This rule provides no automatic fixes or suggestions.
Options
ignoreBin
Defaults to false. Set it to true to allow top-level await in files listed
in package.json's bin field or whose source starts with #!/usr/bin/env.
convertPath
Map source paths to published paths before checking publication and bin.
For example, when package.json publishes lib, check matching TypeScript
sources with:
The first matching array entry wins. An object mapping patterns to
[regularExpression, replacement] pairs is also supported. Omit the option to
use settings.n.convertPath, then settings.node.convertPath, or no conversion.
The replacement uses JavaScript regular expressions and capture substitutions.
Differences from upstream
- JavaScript files without module syntax. In
.jsfiles outside a TypeScript project,sourceType: 'module'can still missawait (value),await [value],await ({ value }), andawait `value`. Addexport {}or use an.mjsfile to make these expressions checked. - Awaiting a negation. In JavaScript,
await !value;can produce a syntax error instead of this rule's diagnostic. Writeawait (!value);in a file withexport {}or an.mjsextension to check it. - Parenthesized operands in computed names. A top-level expression such as
const object = { [await (keyPromise)]() {} };can be missed. Use[await keyPromise], or moveconst key = await (keyPromise);before the object or class declaration, to make the await visible to this rule. - Overlapping path conversions. Object-form
convertPathpatterns use alphabetical order. If both**andsrc/**match,**wins even when you wrotesrc/**first; upstream uses insertion order. This can change whether the resulting file is published or treated as an executable. Use the array form when priority matters. - Publication paths. Package-root files such as
README.jsremain checked when linting from another directory. A filename such as..hidden.jsremains inside its package.convertPathkeeps the source package's publication boundary: with"files": ["lib"], a target such aslib/nested/private.jsstays published even iflib/nested/package.jsonlists onlypublic.js. Subdirectory.npmignorefiles, or.gitignorewhen absent, still apply; they can exclude that target. Upstream can use the nested package's metadata with a path relative to the outer package and incorrectly skip the check. For a package directory namedPkg, a converted path../pkg/lib/a.jsremains inside the package on a case-insensitive filesystem. - Unusual filename patterns. In
files,.npmignore, and.gitignore,[!b]oo.jsmatchesfoo.jsbut notboo.js, andcli\?matches a literal question mark. Upstream can select different files for these patterns. Eachfilesentry also stays one pattern:"lib/foo.js\nbar.js"does not publishlib/foo.js, whereas upstream treats the newline as a separator. Use separate array entries for separate filenames. Further escaping and whitespace cases are described in hashbang's file-selection notes. - Malformed patterns and metadata. An unclosed bracket such as
[cli.jsis matched literally. Upstream matches nothing for this pattern in an ignore file and can throw when it appears infiles. A brace range with a zero step, such aslib/cli{1..3..0}.js, includeslib/cli1.jshere but not upstream; use a positive step or list filenames explicitly. An invalidconvertPathregular expression such as[skips the rule for that file, and non-stringbinentries are ignored. Upstream throws in those last two cases. Correct the expression or executable path so the intended files are checked.