diff --git a/lib/classes/PluginManager.js b/lib/classes/PluginManager.js index a5a3b6ecb58..8aeaa89a3a7 100644 --- a/lib/classes/PluginManager.js +++ b/lib/classes/PluginManager.js @@ -3,7 +3,6 @@ const path = require('path'); const config = require('@serverless/utils/config'); const _ = require('lodash'); -const isModuleNotFoundError = require('ncjsm/is-module-not-found-error'); const ServerlessError = require('../serverless-error'); const resolveCliInput = require('../cli/resolve-input'); const getCommandSuggestion = require('../utils/getCommandSuggestion'); @@ -145,30 +144,42 @@ class PluginManager { return this.asyncPluginInit(); } - requireServicePlugin(serviceDir, pluginPath, localPluginsPath) { - if (localPluginsPath && !pluginPath.startsWith('./')) { + requireServicePlugin(serviceDir, pluginPath, legacyLocalPluginsPath) { + if (legacyLocalPluginsPath && !pluginPath.startsWith('./')) { // TODO (BREAKING): Consider removing support for localPluginsPath with next major - const absoluteLocalPluginPath = path.resolve(localPluginsPath, pluginPath); - + const absolutePluginPath = path.resolve(legacyLocalPluginsPath, pluginPath); + + const isLocatedAtLegacyPluginsPath = (() => { + try { + require.resolve(absolutePluginPath); + return true; + } catch { + return false; + } + })(); + if (isLocatedAtLegacyPluginsPath) return require(absolutePluginPath); + } + const serviceDirRequire = getRequire(serviceDir); + const entryFilePath = (() => { try { - return require(absoluteLocalPluginPath); - } catch (error) { - if (!isModuleNotFoundError(error, absoluteLocalPluginPath)) throw error; + return serviceDirRequire.resolve(pluginPath); + } catch { + return null; } - } - try { - const entryFilePath = getRequire(serviceDir).resolve(pluginPath); + })(); + if (entryFilePath) { if (pluginPath.startsWith('./')) { this.localPluginsPaths.push({ resolvedPath: entryFilePath, inputPath: pluginPath }); } return require(entryFilePath); - } catch (error) { - if (!isModuleNotFoundError(error, pluginPath) || pluginPath.startsWith('.')) { - throw error; - } } // Search in "node_modules" in which framework is placed + try { + require.resolve(pluginPath); + } catch { + throw Object.assign(new Error('Plugin not found'), { code: 'PLUGIN_NOT_FOUND' }); + } return require(pluginPath); } @@ -185,7 +196,7 @@ class PluginManager { try { Plugin = this.requireServicePlugin(serviceDir, name, pluginsObject.localPath); } catch (error) { - if (!isModuleNotFoundError(error, name)) throw error; + if (error.code !== 'PLUGIN_NOT_FOUND') throw error; // Plugin not installed if ( diff --git a/lib/configuration/read.js b/lib/configuration/read.js index 74eefcafccf..6667233d3d0 100644 --- a/lib/configuration/read.js +++ b/lib/configuration/read.js @@ -5,7 +5,6 @@ const isPlainObject = require('type/plain-object/is'); const path = require('path'); const fsp = require('fs').promises; const yaml = require('js-yaml'); -const isModuleNotFoundError = require('ncjsm/is-module-not-found-error'); const getRequire = require('../utils/get-require'); const spawn = require('child-process-ext/spawn'); const cloudformationSchema = require('@serverless/utils/cloudformation-schema'); @@ -137,15 +136,17 @@ const parseConfigurationFile = async (configurationPath) => { // fallthrough case '.js': { const configurationEventuallyDeferred = (() => { + try { + require.resolve(configurationPath); + } catch { + throw new ServerlessError( + `Cannot load "${path.basename(configurationPath)}": File not found`, + 'CONFIGURATION_NOT_FOUND' + ); + } try { return require(configurationPath); } catch (error) { - if (isModuleNotFoundError(error, configurationPath)) { - throw new ServerlessError( - `Cannot load "${path.basename(configurationPath)}": File not found`, - 'CONFIGURATION_NOT_FOUND' - ); - } throw new ServerlessError( `Cannot load "${path.basename(configurationPath)}": Initialization error: ${ error && error.stack ? error.stack : error diff --git a/package.json b/package.json index d18b0c713ab..f971e05af01 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,6 @@ "lodash": "^4.17.21", "memoizee": "^0.4.15", "micromatch": "^4.0.4", - "ncjsm": "^4.3.0", "node-fetch": "^2.6.7", "object-hash": "^2.2.0", "open": "^7.4.2", @@ -95,6 +94,7 @@ "log-node": "^8.0.3", "mocha": "^8.4.0", "mock-require": "^3.0.3", + "ncjsm": "^4.3.0", "nyc": "^15.1.0", "pkg": "^5.5.2", "prettier": "^2.5.1",