Node.js v22でimport assertionsが廃止された件

#Astro#Cloudflare#JavaScript#Node.js#TypeScript#Web開発#プログラミング#解説
投稿日:

Node.js v22でimport assertionsが廃止された件について、日本語の記事が見当たらなかったのでまとめてみました。

TL;DR

はじめに

手元の環境をNode.js v21からv22にアップデートしたところ、このブログのプレビュー用のローカルサーバーが動作しなくなりました。そのときに表示されていたエラーメッセージは次のとおりです。

Terminal window
$ npm run dev
> astro dev
11:19:21 PM [vite] Error when evaluating SSR module C:\Users\<userName>\Documents\Robot-Inventor.github.io\astro.config.ts: failed to import "@robot-inventor/regex-syntax"
|- SyntaxError: Unexpected identifier 'assert'
at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
at callTranslator (node:internal/modules/esm/loader:416:14)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:422:30)
at async link (node:internal/modules/esm/module_job:88:21)
[astro] Unable to load your Astro config
Unexpected identifier 'assert'
Stack trace:
at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
at callTranslator (node: internal/modules/esm/loader:416:14)
at async link (node:internal/modules/esm/module_job:88:21)

このエラーメッセージを読むと、import assertions関連でエラーが出ていることが分かります。Windows Sandbox内で詳しく検証したところ、Node.js 21では問題なく動作する一方で、Node.js 22ではエラーが発生することが確認できました。

Node.js v22でimport assertionが廃止された

さらに詳しく調べていたところ、DenoのこちらのIssueにたどり着きました。

Node.js is planning to remove support for assert in v22 (which will be released in April) and Chrome in v126 (which will be released in May).

—— Warn code that uses import assertions · Issue #17944 · denoland/deno

どうやら、Node.js v22ではimport assertionsの廃止が計画されているとのことです。次に、Node.jsのドキュメントを確認したところ、次のような記述がありました。

Import attributes

VersionChanges
v21.0.0, v20.10.0, v18.20.0Switch from Import Assertions to Import Attributes.
v17.1.0, v16.14.0Added in: v17.1.0, v16.14.0

This feature was previously named “Import assertions”, and using the assert keyword instead of with. Any uses in code of the prior assert keyword should be updated to use with instead.

—— Modules: ECMAScript modules | Node.js v22.0.0 Documentation

つまり、import assertionsは廃止され、import attributesに移行する必要があるということです。また、assertwithに変更するだけで対応できるとのことです。

Node.jsのchangelogにも、import assertionが廃止されたことが記載されていました。公式ブログのリリースノートには記載されていなかったので、完全に見逃していました。

Other Notable Changes

  • [25c79f3331] - esm: drop support for import assertions (Nicolò Ribaudo) #52104

—— node/doc/changelogs/CHANGELOG_V22.md at main · nodejs/node

import attributesに移行する方法

import assertionsからimport attributesに移行する方法は、簡単です。assertwithに変更するだけで対応できます。なお、import attributesはNodejs v18.20.0から導入されているため、それ以前のバージョンでは動作しません。また、TypeScriptでのimport attributesのサポートは、TypeScript v5.3から導入されているため、それ以前のバージョンでは動作しないことに注意してください。

1
import packageJson from "../package.json" assert { type: "json" };
2
import packageJson from "../package.json" with { type: "json" };

Cloudflare Pagesでの対応

Cloudflare PagesのデフォルトのNode.jsのバージョンは、記事執筆時点ではv18.17.1になっています。そのため、import attributesが使われているコードをCloudflare Pagesにデプロイしようとすると、Unexpected identifier 'with'というエラーが発生します。このエラーを防ぐには、Cloudflare Pagesで利用するNode.jsのバージョンを上げる必要があります。

Cloudflare PagesのNode.jsのバージョンを変更するには、Cloudflare Pagesプロジェクトの設定画面の[環境変数]タブで、NODE_VERSIONという環境変数を追加し、使用したいNode.jsのバージョンを指定します。たとえば、Node.js v22を使用する場合は、NODE_VERSION22を設定します。必要に応じて、22.0.0のように詳細なバージョンを指定することもできます。

まとめ

Node.js v22でimport assertionsが廃止されたため、import attributesに移行する必要があります。既存のコードはassertwithに変更するだけで対応できます。Node.js v22にアップデートする際は、この点に注意してください。

参考

Twitterのアイコン LINEのアイコン Threadsのアイコン Misskeyのアイコン Misskeyのアイコン
著者のアイコン画像