* as imports all of the exported values of the module and sticks them in an object with the variable name you specified (e.g. view1). Specifying just a variable name in the import only imports the default export and assigns it the the name you specified (e.g. view2). Any other exports in that module are ignored. Given the view1 module import view1 from './view1.js'; would basically be equivalent to import * as temp from './view1.js'; const view1 = temp.default; In fact the first import above a shorthand for import { default as view1 } from './view1.js'; The braces are used for named imports and the default is named "default". Answer from senocular on reddit.com
🌐
GreenSock
gsap.com › installation
Installation | GSAP | Docs & Learning
To do that, simply click "NPM" and then "UMD" in the install helper above and copy the code generated. For example: import { gsap } from "gsap/dist/gsap"; (notice the files are all in the /dist/ subdirectory)
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › import
import - JavaScript | MDN
The module to import from. Only single quoted and double quoted string literals are allowed. The evaluation of the specifier is host-specified. Most hosts align with browsers and resolve the specifiers as URLs relative to the current module URL (see import.meta.url).
Discussions

When saving the file, the extension operation is blocked
It returns to normal after a few minutes. I encounter this frequently; I’ve been using Cursor for a year and have experienced it at least 20 times. Steps to Reproduce Install “Remove Unused Import”, “Auto Import - ES6, TS, JS”, and “Tailwind CSS IntelliSense”, an... More on forum.cursor.com
🌐 forum.cursor.com
0
0
4 weeks ago
(ES6) What is the difference between "import * as" compared to importing a class?
* as imports all of the exported values of the module and sticks them in an object with the variable name you specified (e.g. view1). Specifying just a variable name in the import only imports the default export and assigns it the the name you specified (e.g. view2). Any other exports in that module are ignored. Given the view1 module import view1 from './view1.js'; would basically be equivalent to import * as temp from './view1.js'; const view1 = temp.default; In fact the first import above a shorthand for import { default as view1 } from './view1.js'; The braces are used for named imports and the default is named "default". More on reddit.com
🌐 r/learnjavascript
8
9
June 23, 2021
Pass options to ES6 module imports
Is it possible to pass options to ES6 imports? How do you translate this: var x = require('module')(someoptions); to ES6? More on stackoverflow.com
🌐 stackoverflow.com
es6 modules - How can I use an ES6 import in Node.js? - Stack Overflow
I'm trying to get the hang of ES6 imports in Node.js and am trying to use the syntax provided in this example: Cheatsheet Link I'm looking through the support table, but I was not able to find what More on stackoverflow.com
🌐 stackoverflow.com
🌐
JavaScript.info
javascript.info
The Modern JavaScript Tutorial
Export and Import · Dynamic imports · Miscellaneous · Proxy and Reflect · Eval: run a code string · Currying · Reference Type · BigInt · Unicode, String internals · WeakRef and FinalizationRegistry · Learning how to manage the browser page: add elements, manipulate their size and position, dynamically create interfaces and interact with the visitor.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-modules-es6
ES6 Modules and How to Use Import and Export in ...
September 4, 2020 - With ES2015 (ES6), with get built-in support for modules in JavaScript. Like with CommonJS, each file is its own module. To make objects, functions, classes or variables available to the outside world it’s as simple as exporting them and then importing them where needed in other files.
🌐
Cursor
forum.cursor.com › support › bug reports
When saving the file, the extension operation is blocked - Bug Reports - Cursor - Community Forum
4 weeks ago - It returns to normal after a few minutes. I encounter this frequently; I’ve been using Cursor for a year and have experienced it at least 20 times. Steps to Reproduce Install “Remove Unused Import”, “Auto Import - ES6, TS, JS”, and “Tailwind CSS IntelliSense”, an...
🌐
Reddit
reddit.com › r/learnjavascript › (es6) what is the difference between "import * as" compared to importing a class?
r/learnjavascript on Reddit: (ES6) What is the difference between "import * as" compared to importing a class?
June 23, 2021 -

(Note: assuming both view1 and view2 have the same functions/methods and variables/properties)

What is the difference between

import * as view1 from './view1.js';

which has a bunch of function and variables being exported.

----------

That being compared to

import view2 from './view2.js';

which exports one object initialized from an object initialized from a function constructor that contains functions(methods) and variables(properties).

I was learning about mvc architecture from a video and they used a function constructor instead of exporting a bunch of functions and variables. Is there an advantage to this?

Find elsewhere
🌐
Todayilearned
todayilearned.net › 2021 › 06 › importing-es6-modules-dynamically
Importing ES6 modules dynamically | Today I Learned
June 10, 2021 - You can import ES6-style JavaScript modules dynamically using await import(filename);. For example: const {default: renamedDefaultExport, namedExport1, namedExport2} = await import("./mymodule.js");
🌐
Vite
vite.dev › guide › features
Features | Vite
It is because Oxc transformer only performs transpilation without type information, it doesn't support certain features like const enum and implicit type-only imports.
Top answer
1 of 9
137

There is no way to do this with a single import statement, it does not allow for invocations.

So you wouldn't call it directly, but you can basically do just the same what commonjs does with default exports:

// module.js
export default function(options) {
    return {
        // actual module
    }
}

// main.js
import m from 'module';
var x = m(someoptions);

Alternatively, if you use a module loader that supports monadic promises, you might be able to do something like

System.import('module').ap(someoptions).then(function(x) {
    …
});

With the new import operator it might become

const promise = import('module').then(m => m.default(someoptions));

or

const x = (await import('module')).default(someoptions)

however you probably don't want a dynamic import but a static one.

2 of 9
28

Concept

Here's my solution using ES6

Very much inline with @Bergi's response, this is the "template" I use when creating imports that need parameters passed for class declarations. This is used on an isomorphic framework I'm writing, so will work with a transpiler in the browser and in node.js (I use Babel with Webpack):

./MyClass.js

export default (Param1, Param2) => class MyClass {
    constructor(){
        console.log( Param1 );
    }
}

./main.js

import MyClassFactory from './MyClass.js';

let MyClass = MyClassFactory('foo', 'bar');

let myInstance = new MyClass();

The above will output foo in a console

EDIT

Real World Example

For a real world example, I'm using this to pass in a namespace for accessing other classes and instances within a framework. Because we're simply creating a function and passing the object in as an argument, we can use it with our class declaration likeso:

export default (UIFramework) => class MyView extends UIFramework.Type.View {
    getModels() {
        // ...
        UIFramework.Models.getModelsForView( this._models );
        // ...
    }
}

The importation is a bit more complicated and automagical in my case given that it's an entire framework, but essentially this is what is happening:

// ...
getView( viewName ){
    //...
    const ViewFactory = require(viewFileLoc);
    const View = ViewFactory(this);
    return new View();
}
// ...

I hope this helps!

🌐
Medium
medium.com › @rahul.jindal57 › es6-imports-vs-commonjs-imports-8e9b66aa04bd
ES6 imports vs CommonJS imports
May 20, 2023 - ES6 imports vs CommonJS: Syntax, performance, Node.js compatibility, and more. Choose the right import method for your project. Read now!
🌐
Medium
medium.com › @vino7tech › es6-modules-importing-exporting-and-organizing-javascript-code-e869676fe372
ES6 Modules: Importing, Exporting, and Organizing JavaScript Code | by Vinotech | Medium
October 15, 2024 - In JavaScript (specifically ES6), modules allow you to organize your code into separate files and share code between them. Modules help manage large codebases by dividing functionality into smaller, reusable components. JavaScript modules can export functionality (like variables, functions, objects, or classes) from one file and import it into another.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › es6-import-and-export
ES6 Import and Export - GeeksforGeeks
January 3, 2023 - In a module, there can be classes, ... file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module....
🌐
Mozilla Discourse
discourse.mozilla.org › t › using-es6-import-on-extension-pages › 61775
Using es6 import on Extension Pages - Development - Mozilla Discourse
June 11, 2020 - Dear team I have implemented an add-on which creates an extension page via tabs.create When running extension in debug temporary extension mode everything works fine. However when web-ext lint is executed I get errors on all pages containing import or exports.
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › how-to-use-an-es6-import-in-node-js
How To Use An ES6 Import In Node.js? - GeeksforGeeks
July 23, 2025 - To enable the use of ES6, we need to make some changes to the package.json file. Before following the steps make sure that Node.js is installed. ... Use the import statement to include modules, ensuring file extensions like .js are explicitly specified.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
What does "./" mean in an ES6 import statement?
April 3, 2019 - In the context of linking to files in HTML and when using the command line, I’m aware “./” just means the same directory and this can be omitted when linking to a file in HTML, but I’m not sure why this is needed/what it means exactly in an ES6 import statement.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Modules
JavaScript modules - JavaScript | MDN
2 weeks ago - The functionality we've exported so far has been comprised of named exports — each item (be it a function, const, etc.) has been referred to by its name upon export, and that name has been used to refer to it on import as well. There is also a type of export called the default export — this is designed to make it easy to have a default function provided by a module, and also helps JavaScript modules to interoperate with existing CommonJS and AMD module systems (as explained nicely in ES6 In Depth: Modules by Jason Orendorff; search for "Default exports").
🌐
W3Schools
w3schools.com › react › react_es6_modules.asp
React ES6 Modules
ES Modules rely on the import and export statements.
🌐
W3Schools
w3schools.com › js › js_modules.asp
W3Schools.com
Modules are code blocks that can export and/or import functions and values.