prettier-plugin-shorten-imports
raw JSON → 0.0.3 verified Mon Apr 27 auth: no javascript
Prettier plugin that rewrites local import and export specifiers to the shortest path depth based on tsconfig.json or jsconfig.json paths mappings. Version 0.0.3, released early 2023, with no further updates since. Supports .js, .jsx, .ts, .tsx, and .vue files, preserving file extensions and normalizing to POSIX separators. It compares relative paths and path aliases, keeping the shortest depth, and skips specifiers resolving to node_modules or outside the project root. Differentiators: integrates with Prettier formatting, avoids long relative paths, respects existing TypeScript/JavaScript path aliases, and merges paths from extended tsconfig chains.
Common errors
error Error: Cannot find module 'typescript' ↓
cause Missing TypeScript peer dependency; plugin requires typescript to parse tsconfig.json.
fix
Install typescript: npm install --save-dev typescript
error Error: The plugin 'prettier-plugin-shorten-imports' requires a Prettier version >= 2.0 ↓
cause Running an older Prettier version that does not meet peer dependency requirement.
fix
Upgrade Prettier: npm install --save-dev prettier@latest
error Error: No tsconfig or jsconfig found for file ↓
cause Plugin cannot locate a tsconfig.json or jsconfig.json in the file's directory tree.
fix
Ensure a tsconfig.json or jsconfig.json exists in the project root or above the target file.
Warnings
gotcha Plugin does not rewrite dynamic imports (import()) or require() calls. ↓
fix Manually update dynamic specifiers if needed; plugin only handles static import/export statements.
gotcha Plugin does not modify specifiers that resolve outside the project root or to node_modules. ↓
fix Ensure specifiers point inside the project; external specifiers are left unchanged.
gotcha For .vue files, only <script> and <script setup> blocks are processed; <script src='...'> external scripts are ignored. ↓
fix If you need to rewrite external script src attributes, handle them separately.
gotcha When multiple tsconfig.ts files exist (via extends), the nearest config's baseUrl and paths take precedence. ↓
fix Ensure the closest tsconfig to the file has the desired paths configuration.
deprecated Version 0.0.3 has not been updated since 2023; may not support Prettier 3+ features or newer TypeScript paths patterns. ↓
fix Consider using an alternative plugin like prettier-plugin-organize-imports or prettier-plugin-import-sort if issues arise.
Install
npm install prettier-plugin-shorten-imports yarn add prettier-plugin-shorten-imports pnpm add prettier-plugin-shorten-imports Imports
- default wrong
const plugin = require('prettier-plugin-shorten-imports')correctimport plugin from 'prettier-plugin-shorten-imports' - default (Prettier config usage) wrong
plugins: [require.resolve('prettier-plugin-shorten-imports')]correctplugins: ['prettier-plugin-shorten-imports'] - default (CLI usage) wrong
prettier --plugin='./node_modules/prettier-plugin-shorten-imports' --write 'src/**/*.ts'correctprettier --plugin=prettier-plugin-shorten-imports --write 'src/**/*.ts'
Quickstart
npm install --save-dev prettier prettier-plugin-shorten-imports typescript
echo '{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@app/*": ["src/*"]
}
}
}' > tsconfig.json
mkdir -p src/utils
echo 'export const formatName = (name: string) => name.toUpperCase();' > src/utils/format.ts
echo "import { formatName } from '../../utils/format';" > src/index.ts
npx prettier --plugin=prettier-plugin-shorten-imports --write src/index.ts
cat src/index.ts