Key Concepts

Review core concepts you need to learn to master this subject

Import Javascript modules with the require function

var moduleA = require( "./module-a.js" ); // The .js extension is optional var moduleA = require( "./module-a" ); // Both ways will produce the same result. // Now the functionality of moduleA can be used console.log(moduleA.someFunctionality)

In Node.js, the require function can used to import code from another file into the current script.

Javascript export default

var moduleA = require( "./module-a.js" ); // The .js extension is optional var moduleA = require( "./module-a" ); // Both ways will produce the same result. // Now the functionality of moduleA can be used console.log(moduleA.someFunctionality)

As of ES6, the export default keywords allow for a single variable or function to be exported, then, in another script, it will be straightforward to import the default export.

After using the export default it is possible to import a variable or function without using the require() function.

Using the import keyword in Javascript

var moduleA = require( "./module-a.js" ); // The .js extension is optional var moduleA = require( "./module-a" ); // Both ways will produce the same result. // Now the functionality of moduleA can be used console.log(moduleA.someFunctionality)

As of ES6, the import keyword can be used to import functions, objects or primitives previously exported into the current script.

There are many ways to use the import keyword, for example, you can import all the exports from a script by using the * selector as follows: import * from 'module_name';.

A single function can be imported with curly brackets as follows: import {funcA} as name from 'module_name';

Or many functions by name: import {funcA, funcB} as name from 'module_name';

Intermediate JavaScript Modules
Lesson 1 of 1
  1. 1
    JavaScript modules are reusable pieces of code that can be exported from one program and imported for use in another program. Modules are particularly useful for a number of reasons. By separati…
  2. 2
    We can get started with modules by defining a module in one file and making the module available for use in another file. Below is an example of how to define a module and how to export it using th…
  3. 3
    To make use of the exported module and the behavior we define within it, we import the module. A common way to do this is with the require() function. For instance, say we want the module to contr…
  4. 4
    We can also wrap any collection of data and functions in an object, and export the object using module.exports. In menu.js, we could write: module.exports = { specialty: “Roasted Beet Burger…
  5. 5
    As of ES6, JavaScript has implemented a new more readable and flexible syntax for exporting modules. These are usually broken down into one of two techniques, default export and named exports. …
  6. 6
    ES6 module syntax also introduces the import keyword for importing objects. In our order.js example, we import an object like this: import Menu from ‘./menu’; 1. The import keyword begins th…
  7. 7
    ES6 introduced a second common approach to export modules. In addition to default export, named exports allow us to export data through the use of variables. Let’s see how this works. In **menu…
  8. 8
    To import objects stored in a variable, we use the import keyword and include the variables in a set of {}. In the order.js file, for example, we would write: import { specialty, isVegetarian…
  9. 9
    Named exports are also distinct in that they can be exported as soon as they are declared, by placing the keyword export in front of variable declarations. In menu.js export let specialty = …
  10. 10
    To import variables that are declared, we simply use the original syntax that describes the variable name. In other words, exporting upon declaration does not have an impact on how we import the va…
  11. 11
    Named exports also conveniently offer a way to change the name of variables when we export or import them. We can do this with the as keyword. Let’s see how this works. In our menu.js example…
  12. 12
    To import named export aliases with the as keyword, we add the aliased variable in our import statement. import { chefsSpecial, isVeg } from ‘./menu’; In orders.js 1. We import chefsSpecia…
  13. 13
    We can also use named exports and default exports together. In menu.js: let specialty = ‘’; function isVegetarian() { }; function isLowSodium() { }; function isGlutenFree() { }; export { sp…
  14. 14
    We can import the collection of objects and functions with the same data. We can use an import keyword to import both types of variables as such: import { specialty, isVegetarian, isLowSodium } …
  15. 15
    We just learned how to use JavaScript modules. Let’s review what we learned: Modules in JavaScript are reusable pieces of code that can be exported from one program and imported for use in anoth…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo