How to Fix "SyntaxError: Cannot use import statement outside of a module" Error in Node

Updated onbyAlan Morel
How to Fix "SyntaxError: Cannot use import statement outside of a module" Error in Node

One of the most confusing parts of the Node ecosystem is that it contains two different module systems, ESM (ECMAScript Modules) and CommonJS. This is a problem because the two module systems are incompatible with each other.

While working in Node, you might come across the following error:

BASH
SyntaxError: Cannot use import statement outside a module

In this post, we'll learn more about the "SyntaxError: Cannot use import statement outside a module" error and how to fix it in Node.

How to fix SyntaxError: Cannot use import statement outside a module

The error "SyntaxError: Cannot use import statement outside a module" is caused by the fact that the file you're trying to import is using the ESM module system, while the file you're trying to import it into is using the CommonJS module system.

For example, let's say you have a file called index.js that uses the CommonJS module system but you try to use the import keyword:

JAVASCRIPT
import { foo } from './foo.js';

This will throw the error "SyntaxError: Cannot use import statement outside a module" because the index.js file is using the CommonJS module system.

JavaScript ESM uses the import and export keywords to include and make available code from other modules, respectively. CommonJS uses the require() function to include code from other modules, and the module.exports object to make code available to other modules.

To fix this, you will either have to continue using the CommonJS module system and use the require keyword:

JAVASCRIPT
const { foo } = require('./foo.js');

Or you will have to convert your entire project to use the ESM module system and use the import keyword:

JAVASCRIPT
import { foo } from './foo.js';

How to convert a CommonJS project to use the ESM module system

To convert your Node project to use the ESM module system, you will need to open your package.json file and add the following property:

JSON
{ "type": "module" }

By setting the type property to module, you are telling Node that your project uses the ESM module system.

After you do this, you will be able to use the import keyword in your project:

JAVASCRIPT
import { foo } from './foo.js';

Conclusion

In this post, we learned about the "SyntaxError: Cannot use import statement outside a module" error and how to fix it in Node.

More specifically, this is caused when you try and mix the ESM and CommonJS module systems in the same project. To fix this, you will either have to continue using the CommonJS module system and use the require keyword or switch over entirely to the ESM module system and use the import keyword.

Thanks for reading!

To learn more about web development, founding a start-up, and bootstrapping a SaaS, follow me on X!
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.