---
title: "How to use relative and absolute paths when importing modules in JavaScript?"  
description: "How to use relative and absolute paths when importing modules in JavaScript?"  
author: "Steilla Mitchel"  
published: 2023-11-01  
updated: 2023-11-02  
canonical: https://www.mindstick.com/forum/160361/how-to-use-relative-and-absolute-paths-when-importing-modules-in-javascript  
category: "javascript"  
tags: ["javascript", "php", "module"]  
reading_time: 2 minutes  

---

# How to use relative and absolute paths when importing modules in JavaScript?

How to use [relative](https://www.mindstick.com/interview/33881/overview-of-relative-absolute-fixed-and-sticky-positioning-in-css) and [absolute](https://www.mindstick.com/interview/1159/what-is-absolute-and-sliding-expiration-in-dot-net) paths when importing [modules](https://www.mindstick.com/interview/266/what-is-the-use-of-http-modules) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?

## Replies

### Reply by Aryan Kumar

In JavaScript, when importing modules using the ES6 module syntax, you can use both relative and absolute paths to specify the location of the module you want to import. Here's how to use both types of paths:

**Relative Paths:** Relative paths specify the location of the module relative to the current module that is doing the importing. You can use relative paths to import modules from the same directory, subdirectories, or parent directories.

To import a module from the same directory:

```plaintext
import myModule from './myModule';
```

To import a module from a subdirectory:

```plaintext
import otherModule from './subdirectory/otherModule';
```

To import a module from a parent directory:

```plaintext
import parentModule from '../parentDirectory/parentModule';
```

**Absolute Paths:** Absolute paths specify the complete path to the module from the root directory of your project. They allow you to import modules from any location in your project without relying on the relative position of the importing module.

To use an absolute path, you typically need to configure your project or module loader to recognize and resolve absolute paths. This can be done through tools like Webpack or Babel with specific plugins.

Example of using an absolute path (assuming project root is the base directory):

```plaintext
import myModule from '/src/myModule';
```

The choice between using relative and absolute paths depends on the project's structure and your preferences. Relative paths are often simpler to use for smaller projects, while absolute paths can provide more flexibility and maintainability in larger projects. However, setting up and maintaining absolute path resolution might require additional configuration and tooling.


---

Original Source: https://www.mindstick.com/forum/160361/how-to-use-relative-and-absolute-paths-when-importing-modules-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
