diff --git a/packages/nx/src/command-line/format/format.ts b/packages/nx/src/command-line/format/format.ts index 5fd7508f2a236f..866f357c21f623 100644 --- a/packages/nx/src/command-line/format/format.ts +++ b/packages/nx/src/command-line/format/format.ts @@ -22,8 +22,9 @@ import { readNxJson } from '../../config/configuration'; import { ProjectGraph } from '../../config/project-graph'; import { chunkify } from '../../utils/chunkify'; import { allFileData } from '../../utils/all-file-data'; +import { gte } from 'semver'; -const PRETTIER_PATH = require.resolve('prettier/bin-prettier'); +const PRETTIER_PATH = getPrettierPath(); export async function format( command: 'check' | 'write', @@ -202,3 +203,15 @@ function sortTsConfig() { // catch noop } } + +function getPrettierPath() { + let prettierVersion: string; + try { + prettierVersion = require(require.resolve('prettier/package.json')).version; + } catch {} + + if (prettierVersion && gte(prettierVersion, '3.0.0')) { + return require.resolve('prettier/bin/prettier.cjs'); + } + return require.resolve('prettier/bin-prettier'); +}