Skip to content

Commit

Permalink
fix(core): improve module not found error (#7267)
Browse files Browse the repository at this point in the history
Co-authored-by: pooya parsa <pooya@pi0.ir>
  • Loading branch information
gion and pooya parsa committed May 27, 2020
1 parent fb33bda commit 36b5c1e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 14 additions & 10 deletions packages/core/src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,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)

message = message.replace(/{name}/g, src)
consola.warn(message)
}

if (this.options._cli) {
throw new Error(message)
throw error
} else {
// TODO: Remove in next major version
message += ' Silently ignoring module as programatic usage detected.'
consola.warn(message)
consola.warn('Silently ignoring module as programatic usage detected.')
return
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,15 @@ 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
})

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

0 comments on commit 36b5c1e

Please sign in to comment.