"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
Matthew C.
—You are trying to import an item such as a function from one JavaScript file into another JavaScript file. For example:
import { toKebabCase } from '/yourFile.js';
After importing this function, you get the following error:
"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
This error occurs because the JavaScript file uses an import
statement in a file that’s not an ES module. ECMAScript is the language specification that JavaScript conforms to. The ECMAScript 6 (ES6), also known as ES2015, specification revision added ES modules to JavaScript. Modules were added to allow programmers to split JavaScript programs into separate modules that allow for easy sharing of items such as variables and functions and better organization of code.
A module is a JavaScript file that exports one or more values using the export
statement. For example:
export const startVal = 2; export function myFunction() { // }
The value can then be imported into another module using the import
statement. For example:
import { startVal, myFunction } from './yourFile.js';
ES modules can make items such as variables, available to other modules that are outside of their scope using an export
statement. Before modules, you would have to move a value to a higher scope, such as the global scope, if you wanted to make it available outside of its scope. Adding values to the global scope can lead to all sorts of issues and makes code hard to maintain. Modules allow for better code organization, sharing of values is explicit. Most modern browsers support ES modules.
Node.js has two module systems: CommonJS modules and ES modules. ES modules were added to Node.js in version 15.3.0.
If your JavaScript file was added to your project using a <script>
tag, add a type
attribute with a value of “module” to your script tag:
<script type="module" src="./yourFile.js"></script>
Node.js, version 15.3.0 and above support ES modules. Older versions use CommonJS only unless you use a library such as esm that allow for ES modules in Node.js version 6 and above. In Node.js versions that support ES modules, you need to set the value of the "type"
field to “module” in the package.json
file:
{ "type": "module" }
Alternatively, you can use the .mjs
file extension to indicate to Node.js that the file is an ES module.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.