close

How can I conditionally import an ES6 module?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How can I conditionally import an ES6 module? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How can I conditionally import an ES6 module ?

  1. How can I conditionally import an ES6 module ?

    can I conditionally import an ES6 module obscuring it in an eval worked for me, hiding it from the static analyzer …
    if (typeof __CLI__ !== 'undefined') { eval("require('fs');") }

  2. How can I conditionally import an ES6 module ?

    can I conditionally import an ES6 module obscuring it in an eval worked for me, hiding it from the static analyzer …
    if (typeof __CLI__ !== 'undefined') { eval("require('fs');") }

Method 1

If you’d like, you could use require. This is a way to have a conditional require statement.

let something = null;
let other = null;

if (condition) {
    something = require('something');
    other = require('something').other;
}
if (something && other) {
    something.doStuff();
    other.doOtherStuff();
}

Method 2

obscuring it in an eval worked for me, hiding it from the static analyzer …

if (typeof __CLI__ !== 'undefined') {
  eval("require('fs');")
}

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment