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 <[email protected]>

---------

Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
  • Loading branch information
sapphi-red and lukastaegert authored 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
Original file line number Diff line number Diff line change
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

0 comments on commit 465ae03

Please sign in to comment.