diff --git a/packages/nx/src/command-line/format/format.ts b/packages/nx/src/command-line/format/format.ts index 5fd7508f2a236..efa7c63a50ac6 100644 --- a/packages/nx/src/command-line/format/format.ts +++ b/packages/nx/src/command-line/format/format.ts @@ -12,6 +12,7 @@ import * as yargs from 'yargs'; import * as prettier from 'prettier'; import { sortObjectByKeys } from '../../utils/object-sort'; +import { readModulePackageJson } from '../../utils/package-json'; import { getRootTsConfigFileName, getRootTsConfigPath, @@ -22,8 +23,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 +204,11 @@ function sortTsConfig() { // catch noop } } + +function getPrettierPath() { + const prettierVersion = readModulePackageJson('prettier').packageJson.version; + if (gte(prettierVersion, '3.0.0')) { + return require.resolve('prettier/bin/prettier.cjs'); + } + return require.resolve('prettier/bin-prettier'); +}