LoginSignup
2
0

More than 3 years have passed since last update.

俺のNode.jsでTop-Level awaitが動かない!!なぜだ!?

Posted at

事象

俺の環境でTop-Level awaitが動かない!!なぜだ!?

原因

Top-Level awaitES Moduleの機能でCommon JSは未対応なので動かない。

The await keyword may be used in the top level (outside of async functions) within modules as per the ECMAScript Top-Level await proposal.

訳:awaitキーワードは、ECMAScriptトップレベルawaitプロポーザルに従って、モジュール内のトップレベル(非同期関数の外部)で使用できます。

出典:Node.js v15.3.0 Documentation

Node.jsのバージョン

v14.15.1

サンプルコード

function wait(time) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve();
        }, time);
    })
}

async function waitSync(time){
    console.log('start timer', time);
    await wait(time);
    console.log('time out',time);
}

await waitSync(2000);
console.log('here');

エラーメッセージ

/Users/username/async/index.js:15
await waitSync(2000);
^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)

対応策

パッと思いつく対応は以下の2択

  1. ファイルの拡張子を.mjsに変更する
  2. package.jsontype属性を追加して、値をmoduleにする

現状

Node.js v15.3.0 Documentation

そもそもTop-level awaitのステータスはStability: 1 - Experimentalなので現状ガリガリ使うものでもない。
あくまでv14.8Experimentalのフラグが不要になっただけ

エラーメッセージについて

module: Improves Top-Level await error in cjs

上記PRにて改善案が実装済みSyntaxError: Top-Level await is only supported in ESM.
とちゃんと教えてくれるようになるよ!

やったね!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0