How to perform a “variable” ES6 import?

Not with the import statement. import and export are defined in such a way that they are statically analyzable, so they cannot depend on runtime information. You are looking for the loader API (polyfill), but I’m a bit unclear about the status of the specification: System.import(‘./utils/’ + variableName).then(function(m) { console.log(m); });

How to resolve Node.js ES6 (ESM) Modules with the TypeScript Compiler (TSC). TSC doesn’t emit the correct file-ext

This is a confusing design choice in TypeScript. In the short term you can work around it by specifying the output file: in main.ts specify the .js extension and path: import { testText } from ‘./module1.js’; alert(testText); This will pick up module.ts correctly, but output with the .js extension included. Note that you also need … Read more

What is the defined execution order of ES6 imports?

JavaScript modules are evaluated asynchronously. However, all imports are evaluated prior to the body of module doing the importing. This makes JavaScript modules different from CommonJS modules in Node or <script> tags without the async attribute. JavaScript modules are closer to the AMD spec when it comes to how they are loaded. For more detail, … Read more

How to use namespaces with import in TypeScript

A solution with namespaces (not recommended) To resolve your issue, you can export your namespace: // UtilBase.ts import * as path from “path”; export namespace My.utils { export class UtilBase { protected fixPath(value: string): string { return value.replace(“https://stackoverflow.com/”, path.sep); } } } Then, you should be able to import it: // UtilOne.ts import {My} from … Read more

Inlining ECMAScript Modules in HTML

Hacking Together Our Own import from ‘#id’ Exports/imports between inline scripts aren’t natively supported, but it was a fun exercise to hack together an implementation for my documents. Code-golfed down to a small block, I use it like this: <script type=”module” data-info=”https://stackoverflow.com/a/43834063″>let l,e,t=”script”,p=/(from\s+|import\s+)[‘”](#[\w\-]+)[‘”]/g,x=’textContent’,d=document, s,o;for(o of d.querySelectorAll(t+'[type=inline-module]’))l=d.createElement(t),o .id?l.id=o.id:0,l.type=”module”,l[x]=o[x].replace(p,(u,a,z)=>(e=d.querySelector( t+z+'[type=module][src]’))?a+`/* ${z} */’${e.src}’`:u),l.src=URL.createObjectURL (new Blob([l[x]],{type:’application/java’+t})),o.replaceWith(l)//inline</script> <script type=”inline-module” id=”utils”> … Read more

What does “… resolves to a non-module entity and cannot be imported using this construct” mean?

Why it doesn’t work import * as MC from ‘./MyClass’; This is ES6/ES2015-style import syntax. The exact meaning of this is “Take the module namespace object loaded from ./MyClass and use it locally as MC“. Notably, the “module namespace object” consists only of a plain object with properties. An ES6 module object cannot be invoked … Read more