ES6 import error handling
I am currently using Babel.
I did the following before with require
:
try {
var myModule = require('my-module');
} catch (err) {
// send error to log file
}
However when trying to do this with import
:
try {
import myModule from 'my-module';
} catch (err) {
// send error to log file
}
I get the error:
'import' and 'export' may only appear at the top level
Now I understand that import
is different to require
. From reading Are ES6 module imports hoisted? import
hoists which means the imports are loaded before code execution.
What I did before was that if any requires failed a log was created which alerted me via email (sending logs to logstash etc.). So my question boils down to the following.
How does one handle import errors in a good practice fashion in nodejs? Does such a thing exist?
This talk give it away : https://github.com/ModuleLoader/es-module-loader/issues/280 and agree with what you said.
import only works at the base level. They are static and always load before the module is run.
So you can't do a code check.
But, the good news is that as it's static, it can be analysed, tools like webpack throw errors at build time.
链接地址: http://www.djcxy.com/p/92896.html上一篇: Django,Heroku,boto:直接将文件上传到Google云存储
下一篇: ES6导入错误处理