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?