fix(appium): add npm version check
Also fix duplication of error messages about engines.
Unfortunately this adds another startup delay, but I'm unsure of a better way around it.  I guess bundling our own npm might work!
MODIFY
packages/appium/lib/config.js
packages/appium/lib/config.js
@@ -3,7 +3,6 @@ import _ from 'lodash';
import {system, fs} from '@appium/support';
import axios from 'axios';
import {exec} from 'teen_process';
-import logger from './logger';
import semver from 'semver';
import findUp from 'find-up';
import {getDefaultsForSchema, getAllArgSpecs} from './schema/schema';
@@ -12,6 +11,7 @@ const npmPackage = fs.readPackageJsonFrom(__dirname);
const APPIUM_VER = npmPackage.version;
const MIN_NODE_VERSION = npmPackage.engines.node;
+const MIN_NPM_VERSION = npmPackage.engines.npm;
const GIT_META_ROOT = '.git';
const GIT_BINARY = `git${system.isWindows() ? '.exe' : ''}`;
@@ -28,6 +28,15 @@ function getNodeVersion() {
return /** @type {import('semver').SemVer} */ (semver.coerce(process.version));
}
+/**
+ * Returns version of `npm`
+ * @returns {Promise<string>}
+ */
+async function getNpmVersion() {
+ const {stdout} = await exec(system.isWindows() ? 'npm.cmd' : 'npm', ['--version']);
+ return stdout.trim();
+}
+
/**
* @param {boolean} [useGithubApiFallback]
*/
@@ -133,7 +142,18 @@ function getBuildInfo() {
function checkNodeOk() {
const version = getNodeVersion();
if (!semver.satisfies(version, MIN_NODE_VERSION)) {
- logger.errorAndThrow(`Node version must be ${MIN_NODE_VERSION}. Currently ${version.version}`);
+ throw new Error(
+ `Node version must be at least ${MIN_NODE_VERSION}; current is ${version.version}`
+ );
+ }
+}
+
+export async function checkNpmOk() {
+ const npmVersion = await getNpmVersion();
+ if (!semver.satisfies(npmVersion, MIN_NPM_VERSION)) {
+ throw new Error(
+ `npm version must be at least ${MIN_NPM_VERSION}; current is ${npmVersion}. Run "npm install -g npm" to upgrade.`
+ );
}
}

MODIFY
packages/appium/lib/main.js
packages/appium/lib/main.js
@@ -19,6 +19,7 @@ import {
showBuildInfo,
validateTmpDir,
warnNodeDeprecations,
+ checkNpmOk,
} from './config';
import {readConfigFile} from './config-file';
import {loadExtensions, getActivePlugins, getActiveDrivers} from './extension';
@@ -37,6 +38,7 @@ const {resolveAppiumHome} = env;
async function preflightChecks(args, throwInsteadOfExit = false) {
try {
checkNodeOk();
+ await checkNpmOk();
if (args.longStacktrace) {
require('longjohn').async_trace_limit = -1;
}

fix(appium): add npm version check

Also fix duplication of error messages about engines.

Unfortunately this adds another startup delay, but I'm unsure of a better way around it.  I guess bundling our own npm might work!