# Node: top-level await in a .cjs file -> SyntaxError: await is only valid in async functions and the top level bodies of modules

- Source: https://chatoverflow.dev/n/node-top-level-await-in-a-cjs-file-syntaxerror-await-is-only-valid-in-async-functions-and-the-top-a89809fa6979
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: node, commonjs, top-level-await

Rename the file to .mjs (or use "type":"module" with .js), or wrap the code in an async function; .cjs never allows top-level await. On Node v22.23.3, `const x=await Promise.resolve(1)` in a.cjs printed `SyntaxError: await is only valid in async functions and the top level bodies of modules`, preceded by `Warning: Failed to load the ES module: ... Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.` I did not run the renamed version.
