Using ESM imports in webpack configuration

I'm in the process of creating a webpack application and I am keen on utilizing ESM (ECMAScript Modules) throughout the entire project. This involves configuring the webpack.config file to allow for ESM imports.

Previously, I knew that this could be achieved using Babel. However, npm has now added support for ESM imports without the need for Babel by introducing the

"type": "module"
directive. I have successfully tested this with Express, but when attempting to do the same with Webpack, I encountered the following error:

> ...proyectDirectory/node_modules/webpack-cli/bin/cli.js:93

> require() of ES modules is not supported.

This led me to wonder if there is a version of webpack-cli available that utilizes import statements instead of require calls.

Answer №1

Make sure to upgrade to webpack-cli version 4.5.0 or higher. The latest update includes support for native ESM configuration files. You can find more information about this release here: https://github.com/webpack/webpack-cli/releases/tag/webpack-cli%404.5.0

If your project uses a directory with

{"type": "module"}
in its package.json and a file named webpack.config.js, it will now be compatible.

You can also use the filename webpack.config.mjs to achieve the same result.

Answer №2

When using Webpack CLI, it typically expects a commonJS file format. There is some experimental support for using the .mjs file extension by enabling the experiments.mjs flag, but there have been reported issues with this approach. Alternatively, you can utilize webpack with either no configuration or by using a .cjs config file like so:

./node_modules/.bin/webpack-cli --config webpack.config.cjs

It's important to note that certain tools like Jest may not be compatible with esm (as of the present date), requiring the use of a different test suite if avoiding babel/esm is preferred.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conceal a card once verified within a bootstrap modal upon successful AJAX completion

On my delete page, there are multiple posts with a delete button. When the delete button is clicked, a Bootstrap modal opens asking for confirmation "Are you sure you want to delete this post? YES : NO" If the YES button is clicked, the .click(function(e) ...

Setting up NodeJS modules

I need to set up a NodeJS server on a computer that doesn't have internet access. After copying and executing the .exe file, I realized I need to install some modules. My question is: Can I transfer the modules from my PC or do I have to connect the ...

Encountering a 404 error due to the random inclusion of the word "clear" in the request URL for npm install

Looking at the code below, it's clear that there is an issue with the URL for the npm registry. The correct URL should be https://registry.npmjs.org/ionic, however, the current link is https://registry.npmjs.org/clear/ionic. Despite completely uninsta ...

Creating chained fetch API calls with dependencies in Next.js

I am a novice who is delving into the world of API calls. My goal is to utilize a bible api to retrieve all books, followed by making another call to the same api with a specific book number in order to fetch all chapters for that particular book. After ...

Is it possible to automatically close navigation dropdowns when the screen size changes?

I am using a bootstrap 4 navbar with dropdowns that open with CSS animation/fade-in on desktop, and a separate toggle button for mobile. Everything works fine, but when I open the dropdowns on mobile and then resize the window screen, they remain open whic ...

The options in the dropdown menu vanish when interacting with a jQuery feature that relies on

Recently, I have found AJAX, JSON, and jQuery to be indispensable tools in my coding endeavors. The application I am working on is a replacement for a flawed one, and it is coming along nicely. Despite making progress with the AJAX method, I have encounte ...

Unsure about the approach to handle this PHP/JSON object in Javascript/jQuery

From my understanding, I have generated a JSON object using PHP's json_encode function and displayed it using echo. As a result, I can directly access this object in JavaScript as an object. Here is an example: .done(function(response) { var ...

Adding an Ajax response to a div in HTML: A step-by-step guide

How can I add Ajax response to a div in my HTML code? Below is my ajax script: $(document).ready(function(){ $('.credit,.debit').change(function(){ var value=$(this).val(); $.ajax({ type:"POST", url:" ...

Create a unique component in ReactJS with the react-google-maps package to enhance your user interface

I would like to integrate a custom marker component into the map, but I have observed that using react-google-maps/api does not support rendering custom components. To illustrate this, here is an example of the code I used: const CustomMarker = ({ text }) ...

How to implement multiple conditions with ng-if in HTML

I need to implement a button that is visible only for specific index numbers: <tbody data-ng-repeat="(wholesalerIndex, wholesaler) in wholesalers"> <tr> <td> <but ...

Extracting information from a Postgres query in Node.js

I'm looking for guidance on how to pass the results of a postgres query in Node.js to another function. Can anyone provide an example? ...

Getting a list of connected users on a PeerJS server using Express is simple and straightforward. Follow these steps to

Trying to incorporate PeerJS, a webRTC library, into a game and utilizing their server for user discovery has proven challenging. The goal is to manage a list of connected users, but grappling with the PeerJS server has been difficult. The documentation s ...

Utilizing Ajax to Invoke Wordpress Functions from a Client's Browser

Currently working on setting up a WordPress website integrated with WooCommerce, while also developing an HTML5 app for my small online store. I am looking to utilize Ajax to call WordPress functions like search directly from my HTML5 app and receive the r ...

When inspecting the source map of Foundation grunt/libsass, incorrect paths are displayed

Recently, I set up a new project to experiment with libsass. I installed Foundation, bower, and grunt for the project. Everything was working smoothly with grunt watch. However, when I opened the project in Chrome's DevTools, I noticed that the CS ...

Utilizing Vue JS for applying multiple filters on a single array

I'm currently facing challenges in optimizing my code. I've successfully created dynamically generated objects from an array, implemented a search function, and had a working filter (just one) at some point. However, my attempt to chain the filte ...

Leverage JavaScript to run a snippet of PHP code directly (without utilizing a separate PHP file)

I am looking for a way to integrate PHP into my web page using JavaScript and AJAX. I want the PHP file to be included and executed as if it is part of the native page, allowing me to utilize features like GET requests. <div id="firstAjaxDiv">Defaul ...

What is the process for uploading a JSON file from your local drive?

I am attempting to use jQuery to load a local JSON file. The code seems to be functioning properly, but for some reason, the data is not being made available in an array. $.getJSON("/ajax/data/myjasonfile.json", function(json) { console.log(js ...

Instructions on creating a non-editable text area where only copying and pasting is allowed

I am looking to simply copy and paste content into a textarea without needing to make any edits within the field. <form action="save.php" method="post"> <h3>Text:</h3> <textarea name="text" rows="4" cols="50"></textarea ...

Trigger the ontextchanged() event for an asp:TextBox using Javascript

I have a specific asp:TextBox that is structured in the following way: <asp:TextBox runat="server" AutoPostBack="True" ID="txtG1" ontextchanged="txtG1_TextChanged" onmouseout="javascript:RefreshIt(this)"/> In addition to this, there is a Javascript ...

Activate Bootstrap modal using an anchor tag that links to a valid external URL as a fallback option

To ensure accessibility for users who may have javascript disabled, we follow a company standard of developing our web pages accordingly. Our target demographic still includes those familiar with VCRs blinking 12:00. One particular challenge involves a te ...