Recently, my project switched from using yarn to npm for dependency management due to various issues. In order to prevent confusion and frustration among contributors, I want to display an error message if they attempt to install dependencies with yarn. While this may not be critical since any code contributions will be flagged during the review process, it is still important to inform them early on that yarn is no longer permitted.
It's worth noting that while contributors to this package must use npm, other projects should still have the option to import our code with yarn. This restriction should only impact contributors directly involved with this particular package.
What I've Attempted
I have tried utilizing the engines
field in the package.json
, but unfortunately, this approach did not work as expected. Referencing stackoverflow () revealed that my package would fail to install correctly when used as a dependency by other projects.
Additionally, I experimented with verifying the npm execpath variable within a preinstall script (). However, this method caused issues during installations from external projects because the script was triggered during their installation process as well.
What I'm Seeking
In the preinstall script, I require a means of distinguishing between local contributors working directly on the project and instances where my package is being installed as a dependency. As a result, I need a function to be inserted here:
#!/usr/bin/env node
// This script executes during the npm "preinstall" hook
const isYarn = () => process.env.npm_execpath.includes('yarn');
const isLocalDev = () => /* ??? */;
if (isYarn() && isLocalDev()) {
console.error('Yarn is not permitted for managing dependencies!');
process.exit(1);
}