3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Node.js で import の順序が気になった話

Last updated at Posted at 2025-02-17

Node.js 22 で気になる挙動があったのでメモしておく

まとめ

  • 静的な順序だった雰囲気だったけど,実行時間が反映されるようになった雰囲気がある
    • 単純な実行時間じゃなくて,非同期になった.というか

Node 20 / 22 で import をしてみる

$ for s in node20 node22; do \
  echo "-- $s -- "; \
  for i in `seq 1 10`; do \
    docker compose run -v $(PWD):/app -w /app --rm $s test.js; \
  done; \
done
-- node20 -- 
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
-- node22 -- 
[ 'b', 'a' ]  # <= ここ
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'a', 'b' ]
[ 'b', 'a' ] # <= こk
[ 'a', 'b' ]

付録

details
compose.yml
services:
  node20:
    image: node:20
    command: -v
  node22:
    image: node:22
    command: -v
test.js
const results = []
const f = async () => {
  await Promise.all(['a', 'b'].map(async (i) => {
    await import(`./${i}.js`)
    results.push(i)
    return
  }))
  return
}
f().then(() => {
  console.log(results)
})
$ wc a.js b.js
       0       0       0 a.js
       0       0       0 b.js
       0       0       0 total

Refs

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?