ES6 Import Error Handling

I am currently using Babel.

I did the following 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 an error message:

'import' and 'export' can only be displayed at the top level

Now I understand that import is different from require . Read Read Are ES6 module modules imported? import , which means that the import is loaded before the code runs.

What I did before was that if any request is not executed, a log is created that alerts me by email (sending logs to logstash, etc.). Therefore, my question boils down to the following.

How to handle import errors in good practice mode in nodes? Is there such a thing?

+5
source share
1 answer

This conversation gives back: https://github.com/ModuleLoader/es-module-loader/issues/280 and agrees with what you said.

import works only at a basic level. They are static and are always loaded before starting the module.

So, you cannot perform code verification.

But the good news is that since it is static, it can be analyzed, tools such as web package errors when creating build time.

+2
source

All Articles