JavaScript

JavaScript: fix “SyntaxError: cannot use import statement outside a module”

Advertisements

I recently was working with a third-party library. I ran into the error “Uncaught SyntaxError: cannot use import statement outside a module” when I tried to import a function from the package. Let us look into why it occurred and how I fixed it.

Why is this happening?

The error description “Uncaught SyntaxError: cannot use import statement outside a module” gives us some hints as to what is happening, but it is still unclear what the exact error is. It occurs when we are trying to use “import” inside a non-ES module. The error can occur in both Node.js environments and packages being imported in the browser. You can read more about ES modules in this post if you are unfamiliar with them.

So whenever we try to import a CommonJS module, we will get the error: “Uncaught SyntaxError: cannot use import statement outside a module“.

Now that we understand the why, let us look into potential solutions.

Use CommonJS “require” instead of “import”

Let us assume we were importing the function hello from wisdomgeek.js. Then, the line that was causing the error would have been:

import { hello } from './wisdomgeek.js';
JavaScript

This would then be replaced by:

const { hello } = require('./wisdomgeek.js');
JavaScript

This is the most naive solution, but we cannot natively mix and match between ES modules and CommonJS modules. We can configure our bundler to make it work with both modules, but that can cause more problems than it solves.

Convert the CommonJS package to use ES Modules instead

We want to convert the CommonJS package to use ES Modules without changing its source code. Luckily, there is a way to tell Node that our project uses ES modules no matter what the third-party package is written as. For this, we need to add a property in our package.json file:

{
  "type": "module"
}
JavaScript

After doing this, the import statement should work without any errors.

import { hello } from './wisdomgeek.js';
JavaScript

What about script tags on the front end?

While the above solution works for Node.js, it does not work if we import the package inside a script tag. We can specify the type as an attribute on the script tag to resolve these errors. So, the import:

<script src="wisdomgeek.js"></script>
JavaScript

Becomes:

<script type="module"src="wisdomgeek.js"></script>
JavaScript

Those are all the solutions to solving the “Uncaught SyntaxError: cannot use import statement outside a module” error. Let us know in the comments section if it helped resolve your error.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

View Comments

Share
Published by
Saransh Kataria

Recent Posts

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

2 weeks ago

Native popover API in HTML

Popovers have been a problem that was typically solved by using a third-party solution. But…

3 weeks ago

Node.js 20.6 adds built-in support for .env files

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform…

4 weeks ago

Object destructuring in TypeScript

Object destructuring is a powerful ES 6 feature that can help developers write cleaner code.…

1 month ago

Improve git clone performance in a CI pipeline

Have you felt particularly annoyed by the time it takes to clone a large repository,…

2 months ago

Fix: Hydration failed because the initial UI does not match what was rendered on the server

Within a React or Next.js app, if you encounter the error "Hydration failed because the…

2 months ago
Advertisements