SyntaxError: Cannot use import statement outside a module

The error "SyntaxError: Cannot use import statement outside a module" commonly arises when attempting to use ES6 features, such as imports, in a JavaScript project. This error specifically occurs when using import statements outside of a module context. To resolve this, ensure that you are using ES6 modules and that your code is organized accordingly.

main.js
import math from 'math.js' math.add()
Output:
import statement outside a module

The occurrence of this error can vary depending on whether you're using JavaScript on the server-side with Node.js or on the client-side in a browser. There are multiple factors that can lead to this error, and the solution will depend on how you are importing the module or using script tags. It's important to identify the context in which the error is happening to apply the appropriate solution.

Solutions:

Add type="module" inside the script tag

When dealing with ECMAScript modules and using JavaScript module import statements in a browser, it's essential to inform the browser that a script is a module. To achieve this, you need to include the attribute type="module" within any <script> tags that reference a JavaScript module. By doing this, you enable the smooth importing of modules without encountering errors.

<script type="module" src="./index.js"></script>

Add "type": "module" to your package.json

If you are developing Node.js or React applications and utilizing import statements instead of require to import modules, make sure your package.json file includes the property "type": "module" as illustrated below:

{ "name": "your-app", "type": "module", "dependencies": { // your dependencies } }

This declaration enables ECMAScript modules to be used effectively in your project, allowing seamless integration of import statements.

Use the extension .mjs in your files

When working within a Node.js application and endeavoring to utilize import statements, encountering the "Cannot use import statement outside a module" error is probable. This occurs due to the default lack of support for ES6 import and export statements within Node.js. To address this issue, it's recommended to utilize the .mjs file extension for your modules and subsequently employ the command designed for running such files. By adhering to this approach, you can effectively resolve the encountered error and enable the seamless utilization of import statements in your Node.js environment.

node --experimental-modules filename.mjs

Use import by required

In some cases, you may have to use both import and require statements to load the module properly.

// import { parse } from 'node-html-parser'; parse = require('node-html-parser');

If you find yourself persistently encountering the error even when applying the import statement within a module file, it's imperative to ascertain the compatibility of your JavaScript environment with modules. Certain outdated browsers and earlier versions of Node.js lack native support for modules, necessitating the implementation of a module bundler such as Webpack or rollup.js. These tools facilitate the transformation of your module code into a format that aligns with the capabilities of your environment, thereby circumventing the recurring error and ensuring the seamless functioning of your code.

Conclusion

The "SyntaxError: Cannot use import statement outside a module" error typically arises when attempting to use the import statement outside of an ES6 module context, whether in a browser or Node.js environment. This issue can be resolved by ensuring your environment supports modules, utilizing the appropriate file extensions (.mjs for Node.js), and potentially employing module bundlers like Webpack for compatibility with older environments.