简体   繁体   中英

Publishing an npm package that strictly requires a minimum Node version

If I write a package I am certain requires Node 4 or higher, I don't want it to be installable with older versions.

I know about the package.json engines field, but that's just advisory (only causes a warning). And enginesStrict has been deprecated.

So what can I do?

One idea is to have a preinstall script that checks the Node version and errors if it's not high enough, preventing installation from continuing. Are there any problems with doing that? And is there a better way?

If you want a good experience, make your CLI entrypoint standard ES5 and CommonJS, detect the node version ( process.version ), and print a detailed and helpful message then exit non-zero. Keep in mind your module may get installed with some node version then the user futzes with nvm or their PATH or whatever and then runs your code under a different version. Thus I think failing nicely at run time is the most important thing. You can also choose to fail at install time if you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM