Skip to content

Commit

Permalink
Add friendly error for npm bug (#5267)
Browse files Browse the repository at this point in the history
* Add friendly error for npm bug

* Remove if part from the error message

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>

---------

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
sapphi-red and lukastaegert committed Nov 25, 2023
1 parent 2e94641 commit 465ae03
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion native.js
Expand Up @@ -57,7 +57,20 @@ function getPackageBase() {
}

const localName = `./rollup.${packageBase}.node`;
const { parse, parseAsync, xxhashBase64Url } = require(
const requireWithFriendlyError = id => {
try {
return require(id);
} catch (error) {
throw new Error(
`Cannot find module ${id}. ` +
`npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
'Please try `npm i` again after removing both package-lock.json and node_modules directory.',
{ cause: error }
);
}
};

const { parse, parseAsync, xxhashBase64Url } = requireWithFriendlyError(
existsSync(join(__dirname, localName)) ? localName : `@rollup/rollup-${packageBase}`
);

Expand Down

1 comment on commit 465ae03

@vercel
Copy link

@vercel vercel bot commented on 465ae03 Nov 25, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.