I have an import something like this:
import { foo } from 'bar';
Is there a way to get Webpack to throw an error if foo
is not defined?
Note that I'm using Babel if that makes a difference.
As Tobias K pointed out in the comments, the other answer is incorrect. Configuring strictModuleExceptionHandling: true
will not produce a compile time error if you try to import a module which does not exist.
The correct configuration is strictExportPresence: true
, which is only available in webpack v2.3.0 and later. (Earlier versions can only show a warning, not an error.)
It is possible to configure webpack 2 to throw an error when an import fails by using output.strictModuleExceptionHandling
. The functionality was added by this pull request https://github.com/webpack/webpack/pull/3302 but it has not been documented yet. Here's how to use it:
module.exports = {
entry: {
main: "./main.js",
},
output: {
filename: "[name].bundle.js",
strictModuleExceptionHandling: true
}
}
Now if I try to import from a file which dosn't exist, or I make an import which resolves to undefined
I would get error and warning messages in the webpack console:
WARNING in ./js/pedigree.js
32:35-49 "export 'default' (imported as 'DisorderLegend') was not found in './disorderLegend'
ERROR in ./js/pedigree.js
Module not found: Error: Can't resolve './OkCancelDialogue' in '/home/tim/workspace/projects/public/js/ext-lib/panogram/js'
@ ./js/pedigree.js 5:0-54
@ ./js/viewerPedigree.js
@ ./main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 ./main.js
webpack: Failed to compile.
In the chrome console you'll get a warning like this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With