fix(core): improve module not found error (#7267)

Co-authored-by: pooya parsa <pooya@pi0.ir>
This commit is contained in:
Bogdan Gradinariu 2020-05-28 00:26:59 +03:00 committed by pooya parsa
parent 7d0da3ff5f
commit 3082567200
2 changed files with 17 additions and 12 deletions

View File

@ -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
}
}

View File

@ -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.')
})
})