Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Execute (import) a string with JavaScript code, that imports ES6 modules, in a browser

I'm making a dynamic system that needs to execute JS code that is generated on the fly in a browser app (Edge or Chrome latest). From another example I've gotten this far:

async function doIt()
{
        let code = "";
        code += "import { MyClass } from './MyClass.js' ;\n";
        code += 'export default function hello() { console.log( "Hello World" ); console.log( MyClass.test() ); }';
        const dataUri = 'data:text/javascript;charset=utf-8,' + encodeURIComponent(code);
        let module = await import(dataUri);
        console.log(module); // property default contains function hello now
        const myHello = module.default;
        myHello(); // puts "Hello World" to console
}

Where MyClass.js has a trivial ES6 module class:

export class MyClass {
    static test() { return 42; }
}

When I run the doIt() function without the second line with the import statement, it runs fine until it tries to call MyClass.test() which is unknown then, of course. With the 2nd import line enabled, it gives the error in Edge or Chrome:

    "Uncaught TypeError: Failed to resolve module specifier './MyClass.js'. 
    Invalid relative url or base scheme isn't hierarchical."

I've tried to make an absolute path of the module reference, but the error remains the same. . So, how to make this work? Or maybe an alternative solution? But using ES6 modules is a hard requirement.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To answer my own question: import() seems to be somewhat crippled, but a workaround is to place the desired dynamic JavaScript in the innerHTML of a new temporary script node in the current HTML page. From there, the ES6 import statements are executed just fine:

        var script = document.createElement( "script" );
        script.setAttribute( 'type', 'module' );
        script.innerHTML = 'import { MyClass } from "/App/MyClass.js"; \
                            someGlobalVar = MyClass.test();

Since it is running at the global scope, you need to store results in some global object. It could be refined by implementing a callback function on an object instance, but I leave that as an excercise for the reader. :-)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda