Run Prettier command on Deno

Since v1.26, Deno introduces npm improvements without older --compat option.

For example, Prettier can run on Deno with a wrapper as follows:

#!/usr/bin/env -S deno run --unstable --allow-read --allow-env --allow-write --allow-sys

import("npm:prettier/cli.js")
  .then(cli => cli.run(Deno.args));

This is rewritten from its node.js version bundled with Prettier npm.

Since a wrapper runs outside of node polyfilled env, it needs to be written in a Deno way:

  • use shebang for deno with lengthy options
  • use import() with npm: resolution instead of require()
  • replace process.argv with Deno.args

Original script has an extra version check, removed because it depends on Node.js.

Save the above as “prettier” with exec bits, and you can run on Deno.

⁋ Nov 10, 2022↻ Dec 18, 2024
中馬崇尋
Chuma Takahiro