From 3082567200c1d7db967471623a1323e7fbe60604 Mon Sep 17 00:00:00 2001 From: Bogdan Gradinariu Date: Thu, 28 May 2020 00:26:59 +0300 Subject: [PATCH] fix(core): improve module not found error (#7267) Co-authored-by: pooya parsa --- packages/core/src/module.js | 24 ++++++++++++++---------- packages/core/test/module.test.js | 5 +++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/core/src/module.js b/packages/core/src/module.js index 237c49eca2..1b578433cb 100644 --- a/packages/core/src/module.js +++ b/packages/core/src/module.js @@ -160,22 +160,26 @@ export default class ModuleContainer { throw error } - let message = 'Module `{name}` not found.' + // Hint only if entrypoint is not found and src is not local alias or path + if (error.message.includes(src) && !/^[~.]|^@\//.test(src)) { + let message = 'Module `{name}` not found.' - if (this.options.buildModules.includes(src)) { - message += ' Please ensure `{name}` is in `devDependencies` and installed. HINT: During build step, for npm/yarn, `NODE_ENV=production` or `--production` should NOT be used.'.replace('{name}', src) - } else if (this.options.modules.includes(src)) { - message += ' Please ensure `{name}` is in `dependencies` and installed.' + if (this.options.buildModules.includes(src)) { + message += ' Please ensure `{name}` is in `devDependencies` and installed. HINT: During build step, for npm/yarn, `NODE_ENV=production` or `--production` should NOT be used.'.replace('{name}', src) + } else if (this.options.modules.includes(src)) { + message += ' Please ensure `{name}` is in `dependencies` and installed.' + } + + message = message.replace(/{name}/g, src) + + consola.warn(message) } - message = message.replace(/{name}/g, src) - if (this.options._cli) { - throw new Error(message) + throw error } else { // TODO: Remove in next major version - message += ' Silently ignoring module as programmatic usage detected.' - consola.warn(message) + consola.warn('Silently ignoring module as programatic usage detected.') return } } diff --git a/packages/core/test/module.test.js b/packages/core/test/module.test.js index 3405e95786..90c2a0d029 100644 --- a/packages/core/test/module.test.js +++ b/packages/core/test/module.test.js @@ -462,7 +462,7 @@ describe('core: module', () => { }) requireModule.mockImplementationOnce(() => { - const moduleNotFound = new Error() + const moduleNotFound = new Error(`Cannot find module 'test-build-module'`) moduleNotFound.code = 'MODULE_NOT_FOUND' throw moduleNotFound }) @@ -470,6 +470,7 @@ describe('core: module', () => { const result = await module.addModule('test-build-module', true) expect(result).toBeUndefined() - expect(consola.warn).toBeCalledWith('Module `test-build-module` not found. Please ensure `test-build-module` is in `devDependencies` and installed. HINT: During build step, for npm/yarn, `NODE_ENV=production` or `--production` should NOT be used. Silently ignoring module as programmatic usage detected.') + expect(consola.warn).toBeCalledWith('Module `test-build-module` not found. Please ensure `test-build-module` is in `devDependencies` and installed. HINT: During build step, for npm/yarn, `NODE_ENV=production` or `--production` should NOT be used.') + expect(consola.warn).toBeCalledWith('Silently ignoring module as programatic usage detected.') }) })