Is there a way using ES2015 (using the import
syntax) to import a module only if it exists?
For example, I want to use a natively-compiled module if it can be installed, but fallback to a pure-js module if it fails for any reason.
I had assumed the following would work:
let crc32;
try {
import Sse4Crc32 from 'sse4_crc32';
crc32 = Sse4Crc32.calculate;
} catch (e) {
import crcJs from 'crc';
crc32 = crcJs;
}
However it gives the error 'import' and 'export' may only appear at the top level
. Is there a way to do this using the import
syntax in ES2015?