# How To Fix The Error: [ERR_UNSUPPORTED_DIR_IMPORT] In Node.js

In this article, we share with you some methods to help you get rid of the error [ERR_UNSUPPORTED_DIR_IMPORT] in Node.js. You can use the experimental flags or specify the full path when importing files to overcome this problem. Let’s find out the exact cause of the error as well as the details of the above methods.

## Why do we get this error?

We will get the error message: “Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import is not supported” when we try to use a directory import in our project.

For ease of follow-up throughout this article, I will reproduce the error case.

Let’s say you have an utilities.js file that exports some functions and variables.

// Export functions
export function square(a) {
return a*a;
}

export function cubed(a) {
return a * a *a;
}

// Export variable
const message = 'Welcome to the LearnShareIT community';
export default message

Then you want to import from file utilities.js. Here I create the test.js file to do that.

// Import from file utilities.js
import message, {square, cubed} from './';

const res1 = square(5);
const res2 = cubed(8);
console.log(res1)
console.log(res2)
console.log(message)

Now you will see the error occurs when executing the main.js file:

## Solutions for the error [ERR_UNSUPPORTED_DIR_IMPORT] in Node.js

### Specify the full path

To get around this issue, make sure you specify the exact path to the imported file.

The reason why we have to specify the full path explicitly is that in Nodejs there is no support for importing directories when resolving ES modules.

// Specify the full path to file utilities.js
import message, {square, cubed} from './utilities.js';

const res1 = square(5);
const res2 = cubed(8);
console.log(res1)
console.log(res2)
console.log(message)

Output:

25
512
Welcome to the LearnShareIT community

In this particular example, files test.js and utilities.js have the same level in the directory structure. So we declare the path from test.js to utilities.js as follows: ‘./utilities.js‘. Ensure you enter the correct path to the imported file when working on your project.

### Use the experimental flags

The second approach for you is using the –experimental-specifier-resolution=node flag.

It allows you to import from the directory containing the index file. Now you will execute the file with the following command:

node --experimental-specifier-resolution=node #filename

Continuing with the first example, you do not need to declare the path when using the –experimental-specifier-resolution=node flag.

// Import from file utilities.js
import message, {square, cubed} from './';

const res1 = square(5);
const res2 = cubed(8);
console.log(res1)
console.log(res2)
console.log(message)

Run the following command to execute test.js:

node --experimental-specifier-resolution=node test.js

Output:

25
512
Welcome to the LearnShareIT community

However, this is the method that we do not recommend you use – Depending on experimental flags is very likely to cause problems in the future. For example, this flag may no longer exist when Nodejs is updated to a newer version.

## Summary

In summary, through this article, we have introduced you to two ways to fix the error [ERR_UNSUPPORTED_DIR_IMPORT] in Node.js. The solution we prefer to use is specifying the full path of modules. Hopefully, our information will be helpful to you.

Maybe you are interested: