Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixi Cannot find module 'prettier-eslint'(#216) #224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import * as path from 'path';
import { createSyncFn } from 'synckit';
import requireRelative from 'require-relative';
import * as os from 'os';

import isFilePathMatchedByIgnore from './ignore';

Expand All @@ -29,6 +30,28 @@ function getModulePath(filePath, moduleName) {
}
}

/**
* @description Get the global node_modules path
* @returns The global node_modules path
*/
function getGlobalNodeModulesPath() {
// Detect the operating system
switch (os.platform()) {
case 'win32':
// Windows
return path.join(process.env.APPDATA, 'npm', 'node_modules');
case 'darwin':
// macOS
return path.join(process.env.PREFIX || '/usr/local', 'lib', 'node_modules');
case 'linux':
// Linux
return path.join(process.env.PREFIX || '/usr', 'lib', 'node_modules');
default:
// Unsupported platform
throw new Error('Unsupported platform');
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global node_modules location is customizable. For example, with nvm the location is something like, ~/.nvm/versions/node/v20.10.0/lib/node_modules.

It also varies with each package manager. You can check the location though:

npm: npm root -g
yarn: yarn global dir
pnpm: pnpm bin -g

/**
* Formats the given document using 'prettier-eslint' package.
* If the document is ignored by either Prettier or ESLint,
Expand Down Expand Up @@ -60,7 +83,7 @@ function formatter(document) {

const formatted = formatText({
text,
prettierEslintPath: getModulePath(document.fileName, 'prettier-eslint'),
prettierEslintPath: getModulePath(getGlobalPackagePath(), 'prettier-eslint'),
filePath: document.fileName,
extensionConfig: { prettierLast },
});
Expand Down Expand Up @@ -121,7 +144,7 @@ function waitForActiveSupportedDocument() {
* by 'prettier-eslint' to resolve its internal dependencies (eslint and prettier).
*/
async function warmUpWorker(document) {
const prettierEslintPath = getModulePath(document.fileName, 'prettier-eslint');
const prettierEslintPath = getModulePath(getGlobalPackagePath(), 'prettier-eslint');

formatText({
text: '',
Expand Down