HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How can I alias a default import in JavaScript?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howaliasimportcanjavascriptdefault

Problem

Using ES6 modules, I know I can alias a named import:

import { foo as bar } from 'my-module';


And I know I can import a default import:

import defaultMember from 'my-module';


I'd like to alias a default import and I had thought the following would work:

import defaultMember as alias from 'my-module';


But that results in a parsing (syntax) error.

How can I (or can I?) alias a default import?

Solution

defaultMember already is an alias - it doesn't need to be the name of the exported function/thing. Just do

import alias from 'my-module';


Alternatively you can do

import {default as alias} from 'my-module';


but that's rather esoteric.

Code Snippets

import alias from 'my-module';
import {default as alias} from 'my-module';

Context

Stack Overflow Q#39282253, score: 1825

Revisions (0)

No revisions yet.