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

Renaming with 'as'

Use the 'as' keyword to rename exports and imports to avoid naming conflicts.

Renaming with 'as'

You'll learn how to avoid naming conflicts and improve clarity by renaming module exports and imports.

What's the Problem?

When working with ES modules, you might encounter situations where:

  • An imported name clashes with a variable you've already defined
  • You want to import multiple exports with the same name from different modules
  • You prefer a more descriptive or shorter name for something you're importing or exporting

This is where the as keyword becomes essential.

Renaming Imports

You can rename any named import to whatever you prefer:

import { functionName as myPreferredName } from './module.js';

// Now use it with the new name
myPreferredName();

This is particularly useful when combining exports from different modules:

import { validate as validateUser } from './userModule.js';
import { validate as validateProduct } from './productModule.js';

// No conflict!
validateUser(userData);
validateProduct(productData);

Renaming Exports

You can also rename when exporting, which is helpful when your internal variable name doesn't match what you want to expose:

function internalHelperFunction() {
  // implementation
}

export { internalHelperFunction as helper };

Now other modules will import it as helper, even though internally you still call it internalHelperFunction.

Think of It Like an Alias

The as keyword works like giving someone a nickname. The person is the same, but you're choosing what to call them in your context. The original name doesn't change in the source module—only what you call it locally.

Key Takeaway: Use the as keyword to rename imports and exports, preventing naming conflicts and making your code more readable without changing the original module's implementation.