Deno v2 が来たので何が変わったのか紹介
はじめに
遂に待望のDeno v2がリリースされました。
混乱の元となっていたwindowオブジェクトを削除し、Node.jsとの互換性を高めるためにprocessオブジェクトがグローバルに導入されるなど、多くのアップデートが施されました。
この記事では重要な変更からminorな変更までを追っていきます。
下記のコマンドで、v2にアップグレード出来ます。
$ deno upgrade
大きな変更
 windowオブジェクトの削除と、processオブジェクトの導入
サーバーサイドであるのに、windowオブジェクトが定義されている影響で、ブラウザ環境と誤認してしまうなど、多くの混乱の元となっていたwindowオブジェクトを削除し、
Node.jsとの互換性を高めるためにprocessオブジェクトがグローバルに導入されるアップデートがされました。
これにより、以前からあったprocessがnot definedになる問題が修正され、Astro等のフレームワークとの互換性が飛躍的に向上しました。
Deno v2でwindowオブジェクトを使おうとすると警告が出るようになったので、もしこの機能に依存している人は確認してみてください。
- feat: Show hints when using 
windowglobal (#25805) - feat(cli): give access to 
processglobal everywhere (#25291) 
 deno add
deno addは、従来よりJSRのパッケージを自動でimportsに追加できる機能として紹介されてきましたが、これからはNPM, JSRが厳格に区別され、以下のようなコマンドは使用できなくなります。
$ deno add @hono/hono
$ npx jsr add @hono/hono
代わりに、jsr:の接頭詞を付けて厳格な指定を行う必要があります。
$ deno add jsr:@hono/hono
$ deno add npm:hono
$ npx jsr add jsr:@hono/hono
これは新たなパッケージレジストリを追加するオプションが出来るので、その為の後方互換性を見据えた変更なのではないかと考察しています。
- feat: require jsr prefix for 
deno installanddeno add(#25698) 
 deno remove
deno addの逆で、自動的にimportsから依存関係を削除してくれます。
package.jsonに対しても使用することが可能です。
$ deno add jsr:@std/testing
Added jsr:@std/testing@1.0.2
$ cat deno.json
{
  "imports": { "@std/testing": "jsr:@std/testing@^1.0.2" }
}
$ deno remove @std/testing
Removed @std/testing
$ cat deno.json
{}
 deno install
deno installは過去に、npm i -g nextのような機能として使われていましたが、今回から、deno add ~を自動的に行えるサブコマンドという立ち位置になりました。
例えば以下のようなファイルがあるとします。
// main.ts
import snapshot from "jsr:@std/testing/snapshot";
import { Hono } from "npm:hono";
この時、以下のコマンドを実行すると自動で依存関係をimportsに追加してくれます。
$ deno install --entrypoint main.ts
Download ...
- feat: require jsr prefix for 
deno installanddeno add(#25698) - feat(install): deno install with entrypoint (#25411)
 
 deno fmt
JavaScript/TypeScriptファイルをフォーマットするのに使用できる、便利な deno fmtですが、なんとHTML/CSSやYAML、.astroや .svelteなどもサポートするようです。
- feat(fmt): stabilize CSS, HTML and YAML formatters (#25753)
 - feat(fmt): support vto and njk extensions (#25831)
 
 deno lint
Node固有のルールの追加で互換性の向上、eslintの--fixのようにquickfixが使用できる機能が安定しました。
deno lint --fix
 deno task
package.json内のscriptsも実行できるようになりました。
{
    ...,
    "scripts": {
        "dev": "next dev"
    },
    ...
}
 deno test
node:testで記述されたテストも実行可能になりました。
 deno doc
HTML出力のデザインが改善され、検索機能が強力になりました。
 deno compile
Windowsで動作するexe形式にする時、アイコンとコード署名が可能になりました。
 deno serve
パフォーマンスが大幅に向上されました。
複数のコアで並列にリクエストを処理することが可能です。
deno serve --parallel
 deno init
--libでライブラリ開発用の汎用コードを生成などの機能が追加されました。
詳しくはこちら
 deno jupyter
Jupyter Note Bookとの互換性が向上されました。(画像やグラフ、HTMLのプレビューが可能)
 deno bench
より詳しい測定が出来るようになりました。
詳しくはこちら
 deno coverage
HTMLでのカバレッジ出力がサポートされました。
 import assertion の仕様変更
前から言われていましたが、Web標準の仕様変更に伴い、正式に変更されました。
- import data from "./data.json" assert { type: "json" };
+ import data from "./data.json" with { type: "json" };
- feat: deprecate import assertions (#25281)
 
削除された仕様
 --unstable
以前からあった、不安定なapiを使用するのに必要なフラグです。
今回から個別に指定、もしくはdeno.jsonに変更するということになりました。
 deno bundle
以前は複数のファイルを一つのファイルにバンドルするサブコマンドとして機能していましたが、途中からESbuild等への移行を推奨されることとなりました。
 deno vendor
以前は依存関係のファイルをvendorフォルダに事前に落としておくサブコマンドでしたが、imports mapとの連携が上手く出来ず、削除されました。
 Denoに組み込まれた複数のオブジェクト
Deno.[Tls]Listener.prototype.rid( #25556 )
Deno.{Conn,TlsConn,TcpConn,UnixConn}.prototype.rid( #25446 )
Deno.{Reader,Writer}[Sync]てDeno.Closer( #25524 )
Deno.Buffer( #25441 )
Deno.close()( #25347 )
Deno.ConnectTlsOptions.{certChain,certFile,privateKey} and Deno.ListenTlsOptions.certChain,certFile,keyFile}( #25525 )
Deno.copy()( #25345 )
Deno.customInspect( #25348 )
Deno.fdatasync[Sync]()( #25520 )
Deno.File( #25447 )
Deno.flock[Sync]()( #25350 )
Deno.FsFile.prototype.rid( #25499 )
Deno.fstat[Sync]()( #25351 )
Deno.FsWatcher.prototype.rid( #25444 )
Deno.fsync[Sync]()( #25448 )
Deno.ftruncate[Sync]()( #25412 )
Deno.funlock[Sync]()( #25442 )
Deno.futime[Sync]()( #25252 )
Deno.iter[Sync]()( #25346 )
Deno.read[Sync]()( #25409 )
Deno.readAll[Sync]()( #25386 )
Deno.seek[Sync]()( #25449 )
Deno.Seeker[Sync]( #25551 )
Deno.shutdown()( #25253 )
Deno.write[Sync]()( #25408 )
Deno.writeAll[Sync]()( #25407 )
機能の追加
重要だと思ったものに説明を付けています。
- feat: add 
--allow-importflag (#25469)- HTTP経由でパッケージをインストールするときに必要なフラグだそうです。
 
 - feat: Add a hint on error about 'Relative import path ... not prefixed with
...' (#25430) - feat: Add better error messages for unstable APIs (#25519)
 - feat: Add suggestion for packages using Node-API addons (#25975)
 - feat: Allow importing .cjs files (#25426)
- CommonJSをネイティブにimport出来るように
 
 - feat: default to TS for file extension and support ext flag in more scenarios
(#25472) - feat: deprecate import assertions (#25281)
 - feat: Don't warn about --allow-script when using esbuild (#25894)
 - feat: hide several --unstable-* flags (#25378)
 - feat: improve lockfile v4 to store normalized version constraints and be more
terse (#25247) - feat: improve warnings for deprecations and lifecycle script for npm packages
(#25694) - feat: include version number in all --json based outputs (#25335)
 - feat: lockfile v4 by default (#25165)
 - feat: make 'globalThis.location' a configurable property (#25812)
 - feat: print 
Listening onmessages on stderr instead of stdout (#25491) - feat: remove 
--lock-writeflag (#25214) - feat: require jsr prefix for 
deno installanddeno add(#25698) - feat: require(esm) (#25501)
 - feat: Show hints when using 
windowglobal (#25805) - feat: stabilize 
Deno.createHttpClient()(#25569)- HTTPのタイムアウトの設定やProxyの用途などとして使われる
Deno.createHttpClient()が安定 
 - HTTPのタイムアウトの設定やProxyの用途などとして使われる
 - feat: suggest 
deno install --entry pointinstead ofdeno cache(#25228) - feat: support DENO_LOG env var instead of RUST_LOG (#25356)
 - feat: TypeScript 5.6 and 
npm:@types/node@22(#25614) - feat: Update no-window lint rule (#25486)
 - feat: update warning message for --allow-run with no list (#25693)
 - feat: warn when using 
--allow-runwith no allow list (#25215) - feat(add): Add npm packages to package.json if present (#25477)
 - feat(add): strip package subpath when adding a package (#25419)
 - feat(add/install): Flag to add dev dependency to package.json (#25495)
 - feat(byonm): support 
deno run npm:<package>when package is not in
package.json (#25981) - feat(check): turn on noImplicitOverride (#25695)
 - feat(check): turn on useUnknownInCatchVariables (#25465)
 - feat(cli): evaluate code snippets in JSDoc and markdown (#25220)
- JSDocの中のコードまで評価するように
 
 - feat(cli): give access to 
processglobal everywhere (#25291) - feat(cli): use NotCapable error for permission errors (#25431)
 - feat(config): Node modules option for 2.0 (#25299)
 - feat(ext/crypto): import and export p521 keys (#25789)
 - feat(ext/crypto): X448 support (#26043)
 - feat(ext/kv): configurable limit params (#25174)
 - feat(ext/node): add abort helpers, process & streams fix (#25262)
 - feat(ext/node): add rootCertificates to node:tls (#25707)
 - feat(ext/node): buffer.transcode() (#25972)
 - feat(ext/node): export 'promises' symbol from 'node:timers' (#25589)
 - feat(ext/node): export missing constants from 'zlib' module (#25584)
 - feat(ext/node): export missing symbols from domain, puncode, repl, tls
(#25585) - feat(ext/node): export more symbols from streams and timers/promises (#25582)
 - feat(ext/node): expose ES modules for _ modules (#25588)
 - feat(flags): allow double commas to escape values in path based flags (#25453)
 - feat(flags): support user provided args in repl subcommand (#25605)
 - feat(fmt): better error on malfored HTML files (#25853)
 - feat(fmt): stabilize CSS, HTML and YAML formatters (#25753)
 - feat(fmt): support vto and njk extensions (#25831)
 - feat(fmt): upgrade markup_fmt (#25768)
 - feat(install): deno install with entrypoint (#25411)
 - feat(install): warn repeatedly about not-run lifecycle scripts on explicit
installs (#25878) - feat(lint): add 
no-process-globallint rule (#25709) - feat(lsp): add a message when someone runs 'deno lsp' manually (#26051)
 - feat(lsp): auto-import types with 'import type' (#25662)
 - feat(lsp): html/css/yaml file formatting (#25353)
 - feat(lsp): quick fix for @deno-types="npm:@types/*" (#25954)
 - feat(lsp): turn on useUnknownInCatchVariables (#25474)
 - feat(lsp): unstable setting as list (#25552)
 - feat(permissions): 
Deno.mainModuledoesn't require permissions (#25667) - feat(permissions): allow importing from cdn.jsdelivr.net by default (#26013)
 - feat(serve): Support second parameter in deno serve (#25606)
 - feat(tools/doc): display subitems in symbol overviews where applicable
(#25885) - feat(uninstall): alias to 'deno remove' if -g flag missing (#25461)
 - feat(upgrade): better error message on failure (#25503)
 - feat(upgrade): print info links for Deno 2 RC releases (#25225)
 - feat(upgrade): support LTS release channel (#25123)
- LTS Releaseへのupgradeをサポート
 
 
バグの修正
- fix: better error for Deno.UnsafeWindowSurface, correct HttpClient name, cleanup unused code
 - fix: cjs resolution cases
 - fix: consistent with deno_config and treat 
"experimentalDecorators"as deprecated - fix: eagerly error for specifier with empty version constraint
 - fix: error out if a valid flag is passed before a subcommand
 - fix: Hide ‘deno cache’ from help output
 - fix: panic when require(esm)
 - fix: precompile preserve SVG camelCase attributes
 - fix: remove the typo in the help message
 - fix: Update deno_npm to fix 
deno installwith crossws - fix: update patchver to 0.2
 - fix: update sui to 0.4
 - fix(add/install): default to “latest” tag for npm packages in 
deno add npm:pkg - fix(check): ignore noImplicitOverrides in remote modules
 - fix(check): properly surface dependency errors in types file of js file
 - fix(cli): Default to auto with –node-modules-dir flag
 - fix(cli): Only set allow net flag for deno serve if not already allowed all
 - fix(cli): Warn on not-run lifecycle scripts with global cache
 - fix(compile): support ‘deno compile’ in RC and LTS releases
 - fix(coverage): ignore urls from doc testing
 - fix(doc): surface graph errors as warnings
 - fix(ext/crypto): ensure EC public keys are exported uncompressed
 - fix(ext/crypto): reject empty usages in SubtleCrypto#importKey
 - fix(ext/node): fix process.stdin.pause()
 - fix(ext/node): Fix vm sandbox object panic
 - fix(ext/node): remove unimplemented promiseHook stubs
 - fix(ext/node): stub cpu_info() for OpenBSD
 - fix(ext/web): don’t ignore capture in EventTarget.removeEventListener
 - fix(flags): –allow-all should conflict with lower permissions
 - fix(flags): move some content from docs.deno.com into help output
 - fix(flags): properly error out for urls
 - fix(fmt): –check was broken for CSS, YAML and HTML
 - fix(info): error instead of panic for npm specifiers when using byonm
 - fix(info): move “version” field to top of json output
 - fix(install): compare versions directly to decide whether to create a child node_modules dir for a workspace member
 - fix(install): store tags associated with package in node_modules dir
 - fix(installl): make bin entries executable even if not put in 
node_modules/.bin - fix(lint): correctly handle old jsx in linter
 - fix(no-slow-types): better 
overridehandling - fix(node): Don’t error out if we fail to statically analyze CJS re-export
 - fix(node): implement libuv APIs needed to support 
npm:sqlite3 - fix(node): Include “node” condition during CJS re-export analysis
 - fix(node): Pass NPM_PROCESS_STATE to subprocesses via temp file instead of env var
 - fix(workspace): handle when config has members when specified via –config
 
最後に
こちらからリリース本文を閲覧できます。
リリースがアナウンスされているブログはこちら
関係ない話ですが、Deno v2でロゴがこれに変わると聞いて驚きました。

#FreeJavaScriptという、OracleからJavaScriptの商標を手放させる活動もしているみたいなので、良かったら署名してみてください。
もし役に立ったら、いいねと共有お願いします!
Author: EdamAmex
Discussion