validate-engines.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // This is separate to indicate that it should contain code we expect to work in all versions of node >= 6.
  2. // This is a best effort to catch syntax errors to give users a good error message if they are using a node version that doesn't allow syntax we are using such as private properties, etc.
  3. // This file is linted with ecmaVersion=6 so we don't use invalid syntax, which is set in the .eslintrc.local.json file
  4. const { engines: { node: engines }, version } = require('../../package.json')
  5. const npm = `v${version}`
  6. module.exports = (process, getCli) => {
  7. const node = process.version
  8. const unsupportedMessage = `npm ${npm} does not support Node.js ${node}. This version of npm supports the following node versions: \`${engines}\`. You can find the latest version at https://nodejs.org/.`
  9. const brokenMessage = `ERROR: npm ${npm} is known not to run on Node.js ${node}. This version of npm supports the following node versions: \`${engines}\`. You can find the latest version at https://nodejs.org/.`
  10. // coverage ignored because this is only hit in very unsupported node versions and it's a best effort attempt to show something nice in those cases
  11. /* istanbul ignore next */
  12. const syntaxErrorHandler = (err) => {
  13. if (err instanceof SyntaxError) {
  14. // eslint-disable-next-line no-console
  15. console.error(`${brokenMessage}\n\nERROR:`)
  16. // eslint-disable-next-line no-console
  17. console.error(err)
  18. return process.exit(1)
  19. }
  20. throw err
  21. }
  22. process.on('uncaughtException', syntaxErrorHandler)
  23. process.on('unhandledRejection', syntaxErrorHandler)
  24. // require this only after setting up the error handlers
  25. const cli = getCli()
  26. return cli(process, {
  27. node,
  28. npm,
  29. engines,
  30. unsupportedMessage,
  31. off: () => {
  32. process.off('uncaughtException', syntaxErrorHandler)
  33. process.off('unhandledRejection', syntaxErrorHandler)
  34. },
  35. })
  36. }