-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[Question] How to prevent people from using npm install <package name>
#4895
Comments
Thanks! You are right, I've made this small change to have a "cleaner" package.json file: package.json: "scripts": {
"preinstall": "node tools/preinstall-script.js"
} tools/preinstall-script.js: /**
* Do NOT allow using `npm` as package manager.
*/
if (process.env.npm_execpath.indexOf('yarn') === -1) {
console.error('You must use Yarn to install dependencies:');
console.error(' $ yarn install');
process.exit(1);
} EDIT: Just found this in AMP: https://github.com/ampproject/amphtml/blob/master/build-system/check-package-manager.js |
@SchnWalter I believe the preinstall trick above would also make it impossible for a user to install your package with npm, no? |
As a dependency? I'm not sure. I'm not using this for public projects/libraries that someone would require as a dependency. |
Hi. There's a way to do this already. You add a fake engine version like so in package.json:
Then you add an
Running NPM then raises an error:
|
It doesn't worked here. $ npm --version
6.12.0
$ node --version
v12.13.0 Accordingly with npm website
|
@rafaelfesi, that's for And the configuration works for me:
|
I think pnpm has released the suitable package for this: only-allow |
@hckhanh Neither only-allow nor any other solutions posted here prevent |
You're right actually. My solution doesn't prevent that (though hopefully, the devs suspicions that you should use yarn would already be aroused if they previously tried to do a plain old |
An RFC for having I don't know if there's one yet for the engine check you mention. |
Do you guys have an update on this topic? |
This might be helpful - https://www.freecodecamp.org/news/how-to-force-use-yarn-or-npm/. |
I'm actually the author of that article and here I am looking for a fix/workaround lmao |
The right way going forward would be the |
Hi team, is there a way to adding constraints to package.json for preventing installing packages for certain users kind of access previlige |
@adamscybot thanks for the approach, unfortunately Heroku does not like it 😞 it throws an error when trying to push to a remote to deploy. remote: -----> Installing binaries
remote: engines.node (package.json): ^12.18
remote: engines.npm (package.json): please-use-yarn
remote: engines.yarn (package.json): ^1.17
remote:
remote: Resolving node version ^12.18...
remote: Downloading and installing node 12.22.10...
remote: Bootstrapping npm please-use-yarn (replacing 6.14.16)...
remote: Unable to install npm please-use-yarn; does it exist? Too bad because it was working perfectly! I'll stick to the |
For those looking for an alternative solution, I've just published a tiny package that does the job: enforcepm You don't need to install it, but you need to create a Git pre-commit hook and put this command inside of it: npx enforcepm yarn By the way you can take the "creating a pre-commit hook" thing as an excuse for using tools such as husky or lint-staged. Hope it helps. |
Some hosting providers will attempt to parse package.json and eventually fail when they encounter a key like In order to bypass this issue you can maximize the version of the package manager you would like to exclude from the list, so for example: "engines": {
"npm": ">= 8.0.0",
"yarn": "9999.9.9",
} The above solution worked for Railway which parses package.json. This makes the assumption [email protected] won't happen in at least a million years but if you are skeptical just raise that number even more |
I'm trying to do the reverse just using npm and not yarn but |
Works like a charm for my project, deploying only on netlify and vercel 😎 |
I would note that you should only do this on your projects. If you do this on a package that is intended to be a dependency of another project (outside your control), I found that it forced the consumer to use my manager of choice instead of theirs. |
run `bun init` and configure astro manually to better understand the framework and its capabilities. based on the installation guide from the official astro documentation (https://docs.astro.build/install/manual) additionally: - add `.editorconfig` to enforce consistent coding styles across different editors (especially for missing configuration from prettier) - add `.github/CODEOWNERS` and `.github/FUNDING.yml` to provide github- specific enhancements for the repository - add `.gitignore` based on the `Node.gitignore` template from GitHub - add `.npmrc` and `engines` field in `package.json` to enforce the usage of bun over other package managers (yarnpkg/yarn#4895 (comment)) - add `.vscode/extensions.json` and `.vscode/settings.json` to recommend the astro extension for formatting - add `LICENSE.md` and `README.md` to provide additional context and information about the project - add additional fields in `package.json` to provide more information about the project
run `bun init` and configure astro manually to better understand the framework and its capabilities. based on the installation guide from the official astro documentation (https://docs.astro.build/install/manual) additionally: - add `.editorconfig` to enforce consistent coding styles across different editors (especially for missing configuration from prettier) - add `.github/CODEOWNERS` and `.github/FUNDING.yml` to provide github- specific enhancements for the repository - add `.gitignore` based on the `Node.gitignore` template from GitHub - add `.npmrc` and `engines` field in `package.json` to enforce the usage of bun over other package managers (yarnpkg/yarn#4895 (comment)) - add `.vscode/extensions.json` and `.vscode/settings.json` to recommend the astro extension for formatting - add `LICENSE.md` and `README.md` to provide additional context and information about the project - add additional fields in `package.json` to provide more information about the project
run `bun init` and configure astro manually to better understand the framework and its capabilities. based on the installation guide from the official astro documentation (https://docs.astro.build/install/manual) additionally: - add `.editorconfig` to enforce consistent coding styles across different editors (especially for missing configuration from prettier) - add `.github/CODEOWNERS` and `.github/FUNDING.yml` to provide github- specific enhancements for the repository - add `.gitignore` based on the `Node.gitignore` template from GitHub - add `.npmrc` and `engines` field in `package.json` to enforce the usage of bun over other package managers (yarnpkg/yarn#4895 (comment)) - add `.vscode/extensions.json` and `.vscode/settings.json` to recommend the astro extension for formatting - add `LICENSE.md` and `README.md` to provide additional context and information about the project - add additional fields in `package.json` to provide more information about the project
run `bun init` and configure astro manually to better understand the framework and its capabilities. based on the installation guide from the official astro documentation (https://docs.astro.build/install/manual) additionally: - add `.editorconfig` to enforce consistent coding styles across different editors (especially for missing configuration from prettier) - add `.github/CODEOWNERS` and `.github/FUNDING.yml` to provide github- specific enhancements for the repository - add `.gitignore` based on the `Node.gitignore` template from GitHub - add `.npmrc` and `engines` field in `package.json` to enforce the usage of bun over other package managers (yarnpkg/yarn#4895 (comment)) - add `.vscode/extensions.json` and `.vscode/settings.json` to recommend the astro extension for formatting - add `LICENSE.md` and `README.md` to provide additional context and information about the project - add additional fields in `package.json` to provide more information about the project
By digging the issues I found
to prevent people from running
npm install
But this line fails to prevent people from running
npm install <package name>
Is there any way to prevent people from running
npm install <package name>
?The text was updated successfully, but these errors were encountered: