Course contentsShow
← JavaScript
Lesson 1401 of 2,72331. ModulesFree lesson

The 'import' Keyword

Import values from other modules using the import keyword and specifying the module path.

The 'import' Keyword

What you'll learn: How to bring exported values from other modules into your current file using the import keyword.


What Is import?

The import keyword is the receiving end of ES Modules. While export lets you share code from a module, import lets you bring that code into another module so you can use it. Think of it like ordering items from a warehouse—export makes items available, and import is how you request specific items to be delivered to your location.

Basic Import Syntax

To import named exports from another module, you specify what you want and where it's coming from:

import { functionName, variableName } from './path/to/module.js';

The curly braces {} specify which named exports you want, and the string after from is the module path—where the JavaScript file lives.

Module Paths

The path can be:

  • Relative: './utils.js' (same folder), '../helpers.js' (parent folder)
  • Absolute: '/src/config.js' (from project root)
  • Package name: 'lodash' (from node_modules in Node.js environments)
// Importing from a file in the same directory
import { greet, farewell } from './greetings.js';

// Now you can use them
greet('Alice'); // Works!

What Happens When You Import

When you import, JavaScript:

  1. Loads the module file (if not already loaded)
  2. Executes its code once
  3. Makes the requested exports available in your current scope

Key Takeaway: The import keyword brings exported values from other modules into your file by specifying what to import and the module's path—it's the essential mechanism for consuming modular code.