---
title: Node.js v22でimport assertionsが廃止された件
url: https://roboin.io/article/2024/05/02/nodejs-v22-drops-import-assertion-support/
description: Node.js v22でimport assertionsが廃止された件について、日本語の記事が見当たらなかったのでまとめてみました。
publishedDate: 2024-05-01T16:41:14.000Z
modifiedDate: 2024-05-01T16:41:14.000Z
thumbnail: https://roboin.io/api/og/2024/05/02/nodejs-v22-drops-import-assertion-support/
thumbnailAlt: 記事のサムネイル画像
thumbnailCaption: N/A
---

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

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

## TL;DR

- 今までJSONをimportする際には、import assertionsが使われていた
- Node.js v22でimport assertionsが廃止された
- 既存のコードはimport attributesに移行する必要がある
- `assert`を`with`に変更するだけで対応できる

## はじめに

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

```bash
$ npm run dev

> robot-inventor.github.io@0.0.1 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](https://github.com/denoland/deno/issues/17944#issuecomment-2027005952)

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

> Import attributes
> 
> |Version|Changes|
> |:---|:---|
> |v21.0.0, v20.10.0, v18.20.0|Switch from Import Assertions to Import Attributes.|
> |v17.1.0, v16.14.0|Added 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](https://nodejs.org/docs/latest-v22.x/api/esm.html#import-attributes)

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

Node.jsのchangelogにも、import assertionが廃止されたことが記載されていました。[公式ブログのリリースノート](https://nodejs.org/en/blog/announcements/v22-release-announce)には記載されていなかったので、完全に見逃していました。

> Other Notable Changes
> 
> - [[`25c79f3331`](https://github.com/nodejs/node/commit/25c79f3331)] - **esm**: drop support for import assertions (Nicolò Ribaudo) [#52104](https://github.com/nodejs/node/pull/52104)
> 
> —— [node/doc/changelogs/CHANGELOG\_V22.md at main · nodejs/node](https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V22.md#2024-04-24-version-2200-current-rafaelgss-and-marco-ippolito)

## import attributesに移行する方法

Import assertionsからimport attributesに移行する方法は、簡単です。`assert`を`with`に変更するだけで対応できます。なお、import attributesはNodejs v18.20.0から導入されているため、それ以前のバージョンでは動作しません。また、TypeScriptでのimport attributesのサポートは、[TypeScript v5.3から導入されている](https://devblogs.microsoft.com/typescript/announcing-typescript-5-3/#import-attributes)ため、それ以前のバージョンでは動作しないことに注意してください。

\`\`\`diff lang="javascript" del="assert" ins="with"
-import packageJson from "../package.json" assert { type: "json" };
+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_VERSION`に`22`を設定します。必要に応じて、`22.0.0`のように詳細なバージョンを指定することもできます。

## まとめ

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

## 参考

- [Warn code that uses import assertions · Issue #17944 · denoland/deno](https://github.com/denoland/deno/issues/17944#issuecomment-2027005952)
- [Modules: ECMAScript modules | Node.js v22.0.0 Documentation](https://nodejs.org/docs/latest-v22.x/api/esm.html#import-attributes)
- [Node.js — Node.js 22 is now available!](https://nodejs.org/en/blog/announcements/v22-release-announce)
- [node/doc/changelogs/CHANGELOG\_V22.md at main · nodejs/node](https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V22.md#2024-04-24-version-2200-current-rafaelgss-and-marco-ippolito)
- [Announcing TypeScript 5.3 - TypeScript](https://devblogs.microsoft.com/typescript/announcing-typescript-5-3/#import-attributes)
- [Language support and tools · Cloudflare Pages docs](https://developers.cloudflare.com/pages/configuration/language-support-and-tools/)