<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged webpack</title>
<link>https://coderlegion.com/tag/webpack</link>
<description>Powered by Question2Answer</description>
<item>
<title>Cannot find module '@babel/core'</title>
<link>https://coderlegion.com/22998/cannot-find-module-babel-core</link>
<description>&lt;p&gt;&lt;span style=&quot;color:#000000&quot;&gt;The error message &lt;strong&gt;Cannot find module '@babel/core'&lt;/strong&gt; is a common issue faced by developers who are using the JavaScript transpiler, &lt;strong&gt;Babel&lt;/strong&gt;. This error occurs when the module cannot be found or is not installed in the project's &lt;strong&gt;node_modules&lt;/strong&gt; folder.&lt;/span&gt;&lt;/p&gt;
&lt;h4&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;&lt;strong&gt; Table of Contents: &lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;What is Babel?&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#what1&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;What is @babel/core module?&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The Solutions&lt;/span&gt;&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol1&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 1: Install &lt;/span&gt;&lt;/a&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol2&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;@babel/core&lt;/span&gt;&lt;/a&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol1&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt; package&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol2&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 2: Check @babel/core package version&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol3&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 3: Check missing or outdated Webpack configuration&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol4&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 3: Check .babelrc configuration file&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#con&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The References&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;section&gt;
&lt;a id=&quot;what&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h2&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;What is Babel?&lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;
&lt;p&gt;&lt;strong&gt;Babel&lt;/strong&gt; is a JavaScript transpiler that allows developers to use the latest &lt;strong&gt;JavaScript&lt;/strong&gt; features, even if the environment they are working in does not support them.&lt;/p&gt;
&lt;p&gt;It also allows developers to write code in one language and transpile it into another language that is more widely supported.&lt;/p&gt;
&lt;section&gt;
&lt;a id=&quot;what1&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h2&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;What is @babel/core module?&lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#why&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h2&gt;
&lt;section&gt;

&lt;p&gt;&lt;strong&gt;@babel/core&lt;/strong&gt; is a package that contains the core functionality of &lt;strong&gt;Babel&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is responsible for parsing the code and generating the output. When this package is not found, the transpilation process cannot be completed, resulting in the &quot;&lt;strong&gt;Cannot find module '@babel/core&lt;/strong&gt;'&quot; error.&lt;/p&gt;

&lt;section&gt;
&lt;a id=&quot;sol&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h2&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The Solutions&lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#sol&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;

&lt;section&gt;
&lt;a id=&quot;sol1&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 1:&lt;/span&gt; Install @babel/core package&lt;/strong&gt;&lt;strong&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#sol1&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h3&gt;
&lt;/section&gt;

&lt;p&gt;The simplest and most common solution is to install the &lt;strong&gt;@babel/core&lt;/strong&gt; package to ensure that it is installed.&lt;/p&gt;

&lt;p&gt;Open your terminal and run the following:&lt;/p&gt;

&lt;pre&gt;
&lt;code class=&quot;language-bash&quot;&gt;npm install @babel/core&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This command will install the package in the project's &lt;strong&gt;node_modules&lt;/strong&gt; folder and make it available for use.&lt;/p&gt;

&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;It's always recommended to review the package.json file and make sure all the dependencies and devDependencies are up-to-date.&lt;/span&gt;
&lt;/div&gt;

&lt;section&gt;
&lt;a id=&quot;sol2&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 2:&lt;/span&gt; Install @babel/core latest version &lt;a rel=&quot;nofollow&quot; href=&quot;#sol2&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h3&gt;
&lt;/section&gt;

&lt;p&gt;Another common cause of this error is a missing or outdated version of &lt;strong&gt;Babel&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To ensure that you have the latest version of &lt;strong&gt;Babel&lt;/strong&gt;, you can run the following command:&lt;/p&gt;

&lt;pre&gt;
&lt;code class=&quot;language-bash&quot;&gt;npm install --save-dev @babel/core&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;then the latest version of the &lt;strong&gt;@babel/core&lt;/strong&gt; package will be installed and saved as a development dependency in the project.&lt;/p&gt;

&lt;div class=&quot;div-purple&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Note&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;It's also important to note that, in some cases, the error may be caused by a missing or outdated version of other dependencies that are related to babel, for example, babel-cli, babel-loader, babel-preset-env, babel-polyfill.&lt;/span&gt;
&lt;/div&gt;

&lt;section&gt;
&lt;a id=&quot;sol3&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 3: Check missing or outdated Webpack configuration &lt;a rel=&quot;nofollow&quot; href=&quot;#sol3&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;/section&gt;

&lt;p&gt;It's also possible that the error is caused by a missing or outdated &lt;strong&gt;Webpack&lt;/strong&gt; configuration.&lt;/p&gt;

&lt;div class=&quot;div-purple&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Note&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Webpack is a JavaScript module bundler that takes all of the different modules in a project and generates a single bundle or multiple bundles&lt;/span&gt;
&lt;/div&gt;



&lt;p&gt;In this case, you should check your &lt;strong&gt;webpack.config.js&lt;/strong&gt; file to ensure that it is properly configured to use &lt;strong&gt;Babel &lt;/strong&gt;like the following:&lt;/p&gt;

&lt;pre&gt;
&lt;code class=&quot;language-javascript&quot;&gt;const path = require(&quot;path&quot;);
const babel = require(&quot;@babel/core&quot;);

module.exports = {
  entry: &quot;./src/index.js&quot;,
  output: {
    path: path.resolve(__dirname, &quot;dist&quot;),
    filename: &quot;bundle.js&quot;
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: &quot;babel-loader&quot;,
          options: {
            presets: [&quot;@babel/preset-env&quot;]
          }
        }
      }
    ]
  }
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the above example, the &lt;strong&gt;babel-loader&lt;/strong&gt; is being used to transpile the JavaScript files using the &lt;strong&gt;@babel/preset-env preset&lt;/strong&gt;. This preset allows the code to be transpiled to the version of JavaScript that is supported by the target environment.&lt;/p&gt;

&lt;section&gt;
&lt;a id=&quot;sol4&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h3&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution 4: &lt;/span&gt;Check .babelrc configuration file &lt;a rel=&quot;nofollow&quot; href=&quot;#sol2&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h3&gt;
&lt;/section&gt;

&lt;p&gt;It's also possible that the error is caused by a missing or outdated &lt;strong&gt;.babelrc&lt;/strong&gt; configuration file.&lt;/p&gt;

&lt;p&gt;This file is used to configure &lt;strong&gt;Babel&lt;/strong&gt; and specify the presets and plugins that should be used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.babelrc&lt;/strong&gt; file should contain the following: &lt;/p&gt;

&lt;pre&gt;
&lt;code class=&quot;language-json&quot;&gt;{
  &quot;presets&quot;: [&quot;@babel/preset-env&quot;],
  &quot;plugins&quot;: [&quot;@babel/plugin-proposal-class-properties&quot;]
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;the &lt;strong&gt;@babel/preset-env&lt;/strong&gt; and &lt;strong&gt;@babel/plugin-proposal-class-properties&lt;/strong&gt; are specified as the presets and plugins to be used.&lt;/p&gt;

&lt;div class=&quot;div-pink&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Caution&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Assuming that you are still facing the issue, it's best to check the project's package.json, webpack.config.js, and .babelrc files to ensure that everything is configured correctly. If the problem persists, you can also check the project's documentation or seek help from the community&lt;/span&gt;
&lt;/div&gt;

&lt;section&gt;
&lt;a id=&quot;con&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h2&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The Conclusion&lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#con&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;

&lt;p&gt;In conclusion, the &lt;strong&gt;Cannot find module '@babel/core'&lt;/strong&gt; error is a common issue faced by developers using &lt;strong&gt;Babel&lt;/strong&gt;, but it can be easily resolved by checking and updating the package installations, configuration files, and dependencies. It's important to stay vigilant and maintain your project's dependencies and configurations to avoid these types of errors and ensure smooth development.&lt;/p&gt;

&lt;section&gt;
&lt;a id=&quot;ref&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;

&lt;h2&gt;&lt;strong&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The References&lt;/span&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#ref&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt; &lt;/strong&gt;&lt;/h2&gt;
&lt;/section&gt;

&lt;ul&gt;
&lt;li&gt;The official Babel documentation on troubleshooting:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://babeljs.io/docs/en/troubleshooting#cannot-find-module-babel-core&quot;&gt;
https://babeljs.io/docs/en/troubleshooting#cannot-find-module-babel-core
&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
The official Babel documentation:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://babeljs.io/docs/en/index.html&quot;&gt;https://babeljs.io/docs/en/index.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
The official webpack documentation:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://webpack.js.org/&quot;&gt;https://webpack.js.org/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
The official webpack documentation on configuration:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://webpack.js.org/configuration/&quot;&gt;https://webpack.js.org/configuration/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
The official Babel documentation on configuration:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://babeljs.io/docs/en/configuration&quot;&gt;https://babeljs.io/docs/en/configuration&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;&lt;/section&gt;</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/22998/cannot-find-module-babel-core</guid>
<pubDate>Sun, 19 Jul 2026 06:07:05 +0000</pubDate>
</item>
<item>
<title>Syntaxerror: unexpected token 'export'</title>
<link>https://coderlegion.com/14378/syntaxerror-unexpected-token-export</link>
<description>&lt;p&gt;&lt;strong&gt;The error `&lt;span style=&quot;color:#9b59b6&quot;&gt;Syntaxerror: unexpected token 'export'&lt;/span&gt;` will be encoutered when you try to use the export keyword in a version of nodejs that is prior to v14.13.0. To solve the problem, you have to either use the older method of exporting the method, and that is using the method module.exports, or, you may be better off updating to the latest version of nodejs. I recommend you do the latter as it's better for your system security and number of features. You don't want to stay behind using old cumbersome method of doing things in nodejs.&lt;/strong&gt;&lt;/p&gt;
&lt;div id=&quot;toc-div&quot;&gt;
&lt;h4&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt; Table of Contents: &lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;div id=&quot;toc-list&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot;&gt;Explanation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The Solution&lt;/span&gt;&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol1&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution one&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol2&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;Solution two&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#con&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;a class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;#what&quot; id=&quot;TypeError-str-object&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;The error says that the export token was unexpected and that means the Javascript runtime parser has not been able to understand the export keyword. To understand, how does this error happen. We have to understand first what is NodeJS and how it imports and exports modules.&lt;/p&gt;
&lt;h3&gt;&lt;strong&gt;What is NodeJS?&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;If you are already coding on the client side, you are necessarily using JavaScript. Why? Because all browsers are equipped with a Javascript engine that will allow us to translate our Javascript code into machine code.&lt;/p&gt;
&lt;p&gt;Well, one day, there was a guy called Ryan Dahl who had the brilliant idea of taking the Javascript V8 engine, which is the one found in the Chrome browser, and using it outside the Navigator. He created the Node.js platform!&lt;/p&gt;
&lt;p&gt;Node.js is very frequently used to write server-side services called APIs (Application Programming Interface) And in the end, we can say that Node.js represents an alternative to server languages like PHP, Java or Python. This alternative has become popular in a lot of companies and has even revolutionized a lot of things in big companies like PayPal. PayPal, which used Java, decided to test Node.js on a small part of its code and the results were quite impressive.&lt;/p&gt;
&lt;p&gt;Some real-time applications may have application features that are used in multiple places in that application, but are not available in the NodeJS module repository. To take advantage of reuse in this scenario, you need to create a new NodeJS module. It is not enough to create a new module to be used by other modules in the system. This module must be exported to be reused in other modules. If you're a UI or Java developer, you're probably familiar with it. If your Java-based application contains generic or reusable components, develop it as a separate project, create a Jar file, and add it to your project's required classpath. Don't worry too much about creating NodeJS modules at this point. We will discuss how to create our own NodeJS modules in the future.&lt;/p&gt;
&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;&lt;strong&gt;How to import and export modules in NodeJS? &lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#why&quot; id=&quot;reasons-that-will-cause&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;A “module” is any file composed of Javascript code imported into another file or script.&lt;/p&gt;
&lt;p&gt;Modules allow you to split up your code to better organize it. In practice, when several people are working on a project, it is common to split the code into several files containing related parts of the final program to facilitate code, maintenance, and overall teamwork.&lt;/p&gt;
&lt;p&gt;To export functions or classes, we have to define function or class first and then assign the class or function to a member of the variable &lt;strong&gt;exports&lt;/strong&gt;. Open you Visual Studio code and create new folder for doing this mini-project. Create a new file and name &lt;strong&gt;mymodule.js&lt;/strong&gt;. And type the following inside it:&lt;/p&gt;
&lt;pre&gt;
&lt;code class=&quot;language-javascript&quot;&gt;function add(a,b) {
  return a + b;
}

class User {
    sayHello() {
        console.log(&quot;Hello, I'm user!&quot;)
    }
}

exports.add = add
exports.User = User&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After you are done doing that you have to next create another Javascript file and name it &lt;strong&gt;index.js&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To import these functions and classes in &lt;strong&gt;index.js&lt;/strong&gt; script we have to use the require function which is compatible with &lt;strong&gt;CommonJS &lt;/strong&gt;version we are using at the moment.&lt;/p&gt;
&lt;p&gt;You have to assign the return variable from the &lt;strong&gt;require&lt;/strong&gt; to a variable inside your script to use. And for the &lt;strong&gt;require() &lt;/strong&gt;function, you have to pass the path of the file without the file extension at the end.&lt;/p&gt;
&lt;pre&gt;
&lt;code class=&quot;language-javascript&quot;&gt;const { User, add } = require(&quot;./mymodule&quot;)

// Or you can do it this way
const mymodule = require(&quot;./mymodule&quot;)
// The class will be available at mymodule.User and the add function will be available at mymodule.add
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;&lt;strong&gt;The Solution &lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#sol&quot; id=&quot;the-solution&quot; style=&quot;color: blue&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Now that we know everything regarding how to fix this issue, we will resolve the issue now. There are two main methods for fixing this. First and I advise you do this instead is upgrade to the latest version of NodeJS and the second method is to use &lt;strong&gt;CommonJS &lt;/strong&gt;method of importing function and classes. We will look at each of them right now.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;&lt;strong&gt;Solution one&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;I&lt;/strong&gt;If you are using are using Windows, download the latest package of NodeJS and install it. And if you are using Linux, I advise you install the global package &lt;strong&gt;'&lt;/strong&gt;&lt;strong&gt;n'&lt;/strong&gt;. This package will install everything. After that close and reopen the terminal and the new version will take effect.&lt;/p&gt;
&lt;p&gt;First, let's install &lt;strong&gt;n.&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;
&lt;code class=&quot;language-bash&quot;&gt;npm install -g n&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After it complete installing. Run the following command:&lt;/p&gt;
&lt;pre&gt;
&lt;code class=&quot;language-bash&quot;&gt;n latest&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After that, the new package will be completely installed. Now close the terminal session and reopen it. And run the script again.&lt;/p&gt;
&lt;p&gt;This will resolve the import errors and make the script function properly.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;/p&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;&lt;strong&gt;Solution two&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;Another simple solution is to use the old way of exporting modules, using &lt;strong&gt;CommonJS&lt;/strong&gt;. To import a module in &lt;strong&gt;CommonJS&lt;/strong&gt;, we use the require function and we give the path to the script without the file extension. The file extension is &lt;strong&gt;.js.&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;
&lt;code class=&quot;language-javascript&quot;&gt;class User {
  sayHello() {
    console.log(&quot;Hello, I'm user!&quot;);
  }
}

module.exports.User = User&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After this the export will work fine.&lt;/p&gt;
&lt;p&gt;&lt;a class=&quot;custom_anchor&quot;&gt;&lt;/a&gt; &lt;/p&gt;&lt;h2&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif&quot;&gt;&lt;strong&gt;The Conclusion&lt;/strong&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;Thanks for reading the tutorial and sticking all the way to the end. To sum up, there are two possible fixes for this problem. The best one is to update you NodeJS environment and use the latest method of exporting and importing scripts. The other method is to use the way of doing things and use &lt;strong&gt;CommonJS&lt;/strong&gt; syntax by assigning the classes and functions to the &lt;strong&gt;exports&lt;/strong&gt; variable, then importing them with the &lt;strong&gt;require()&lt;/strong&gt; function.&lt;/p&gt;
&lt;p&gt;That's it for this tutorial. If you run into any issues, don't hesitate to leave a comment.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/14378/syntaxerror-unexpected-token-export</guid>
<pubDate>Thu, 09 Apr 2026 23:39:59 +0000</pubDate>
</item>
<item>
<title>Preloading chunks with webpack</title>
<link>https://coderlegion.com/1087/preloading-chunks-with-webpack</link>
<description>&lt;p&gt;Webpack is a well-established web bundler that is widely known (among other things) for its rich set of features.&lt;/p&gt;
&lt;p&gt;One of the many cool aspects of webpack that I would like to focus on in this article is the concept of &lt;strong&gt;preloading&lt;/strong&gt;, i.e. what it is, why it is needed and how it works.&lt;/p&gt;
&lt;p&gt;Before jumping into the actual topic, we first have to quickly cover some preliminary notions in order to get the most out of this writing.&lt;/p&gt;
&lt;h3 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h3&gt;
&lt;h4 id=&quot;what-is-a-chunk-&quot;&gt;What is a chunk?&lt;/h4&gt;
&lt;p&gt;When talking about webpack, the concept of a &lt;em&gt;chunk&lt;/em&gt; can be hardly skipped over.&lt;/p&gt;
&lt;p&gt;In simple terms, a chunk is a file that is created as a result of the bundling process. &lt;br&gt;
This generated file contains modules (which can be thought of as the initial files and their entire chains of dependencies), as well some &lt;em&gt;runtime code&lt;/em&gt;, which is code added by webpack in order to achieve &lt;em&gt;its magic&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;There are multiple kind of chunks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;entry chunks, i.e. chunks created from entry files, declared in the &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Andrei0872/understanding-webpack/blob/master/examples/chunk-preload/webpack.config.js#L9&quot;&gt;&lt;code&gt;entry&lt;/code&gt; configuration option&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;async chunks, i.e. those created with the &lt;code&gt;import()&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;chunks created automatically by webpack, e.g. when using &lt;a rel=&quot;nofollow&quot; href=&quot;https://andreigatej.dev/blog/webpack-splitchunksplugin/&quot;&gt;webpacks' &lt;code&gt;SplitChunksPlugin&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;I have written in more detail about what chunks are in webpack in this &lt;a rel=&quot;nofollow&quot; href=&quot;https://andreigatej.dev/blog/webpack-what-is-a-chunk/&quot;&gt;previous article&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;the-import-function&quot;&gt;The &lt;code&gt;import()&lt;/code&gt; function&lt;/h4&gt;
&lt;p&gt;When we talk about preloading chunks, it is somehow implied that we are talking about &lt;strong&gt;async chunks&lt;/strong&gt;. This kind of chunks are created with the help of&lt;br&gt;
the &lt;code&gt;import()&lt;/code&gt; function, so it is worth spending a few minutes on this.&lt;/p&gt;
&lt;p&gt;Whenever webpack detects a call to &lt;code&gt;import('file.js')&lt;/code&gt;, webpack will automatically create a chunk that corresponds to &lt;code&gt;file.js&lt;/code&gt;, such that, when that line that invokes the function&lt;br&gt;
is reached, an actual HTTP request will be made and, through this, the file will be fetched and then integrated into the application.&lt;/p&gt;
&lt;p&gt;Why is it needed to talk about this concept before introducing &lt;em&gt;chunk preloading&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;Preloading, in itself, it is a strategy to optimise the experience of the users on the Internet. It narrows down to giving the browser certain instructions that will fetch&lt;br&gt;
assets with higher priority (typically, one would use that for resource that are critical to the first navigation to the website). &lt;br&gt;
When we take into account that &lt;code&gt;import()&lt;/code&gt; essentially creates files (i.e. assets) that will be loaded at a later time, it makes sense to sometimes want to preload such resources with priority.&lt;/p&gt;
&lt;p&gt;We will see how to actually instruct webpack to apply this resource hint in a browser context, as well as other underpinnings of preloading in the following sections.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;As a side note (and as you might have guessed probably), the &lt;code&gt;import()&lt;/code&gt; function can also be used to achieve &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.youtube.com/watch?v=gttkoU8YTkI&amp;amp;list=PL1Qj0WoSxDryPzQ7ZrR6ymu7M5k3mwEiA&amp;amp;index=11&quot;&gt;lazy loading&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;preloading-chunks&quot;&gt;Preloading chunks&lt;/h3&gt;
&lt;p&gt;As hinted at earlier, &lt;em&gt;preloading&lt;/em&gt; is one of the &lt;a rel=&quot;nofollow&quot; href=&quot;https://web.dev/learn/performance/resource-hints#preload&quot;&gt;resource hints&lt;/a&gt; available.&lt;/p&gt;
&lt;p&gt;Applying this to webpack, this means we can preload &lt;strong&gt;async chunks&lt;/strong&gt; (e.g. other JavaScript files) as early as possible. What &lt;em&gt;preloading&lt;/em&gt; means is that the file (i.e. the chunk)&lt;br&gt;
will be &lt;strong&gt;fetched&lt;/strong&gt; over the network, but &lt;strong&gt;not executed&lt;/strong&gt; yet. Instead of being executed, it will be stored in the browser's cache (we will henceforth assume that the file in question is cacheable) until the line that calls&lt;br&gt;
&lt;code&gt;import()&lt;/code&gt; is be reached.&lt;/p&gt;
&lt;p&gt;We can tell webpack to preload an async chunk by using the &lt;code&gt;webpackPreload&lt;/code&gt; magic comment in the &lt;code&gt;import()&lt;/code&gt; function.&lt;br&gt;
So, if we have this line in a file named &lt;code&gt;a.js&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// a.js
import(/* webpackPreload: true */ 'a1.js')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We would expect the file &lt;code&gt;a1.js&lt;/code&gt; to be preloaded. However, as we will see, this will only happen under certain conditions. For instance,&lt;br&gt;
if &lt;code&gt;a.js&lt;/code&gt; is part of an &lt;em&gt;entry&lt;/em&gt; chunk (e.g. for instance, as &lt;code&gt;index.js&lt;/code&gt; is &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Andrei0872/understanding-webpack/blob/master/examples/chunk-preload/webpack.config.js#L9&quot;&gt;here&lt;/a&gt;), then &lt;code&gt;a1.js&lt;/code&gt; &lt;strong&gt;can't be preloaded&lt;/strong&gt;. But, if we had something like this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// index.js - entry file, mentioned in webpack configuration.
import('a.js')

// a.js.
import(/* webpackPreload: true */ 'a1.js')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, when the dynamic import for &lt;code&gt;a.js&lt;/code&gt; takes place, the chunk that corresponds to &lt;code&gt;a1.js&lt;/code&gt; &lt;strong&gt;will be preloaded&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;We will clarify these facts later on in the article. For now, let's get familiar with the &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Andrei0872/understanding-webpack/tree/master/examples/chunk-preload&quot;&gt;demo application&lt;/a&gt; which will help us gain a better understanding of this topic:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;├── a1.js
├── a2.js
├── a.js
├── b.js
├── index.js
└── webpack.config.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only relevant information with respect to the wepback configuration is that the &lt;code&gt;index.js&lt;/code&gt; file is the value of the &lt;a rel=&quot;nofollow&quot; href=&quot;https://webpack.js.org/concepts/entry-points/&quot;&gt;&lt;code&gt;entry&lt;/code&gt; option&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The files - or, to use webpack's parlance, &lt;em&gt;modules&lt;/em&gt; - are not containing any logic more complex that a few simple &lt;strong&gt;dynamic imports&lt;/strong&gt;. This diagram describes how these modules are connected: &lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=15080455495733350335&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;A few clarifications regarding the diagram above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the green bounding rectangles represent &lt;strong&gt;async chunks&lt;/strong&gt;; these chunks are created because of the use of the &lt;code&gt;import()&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;the gray bounding rectangle indicates an &lt;strong&gt;entry chunk&lt;/strong&gt;, i.e. a chunk that will be invariably by the browser&lt;/li&gt;
&lt;li&gt;the yellow contained rectangles represent &lt;strong&gt;modules&lt;/strong&gt; that are part of certain chunks&lt;/li&gt;
&lt;li&gt;&lt;code&gt;a.js&lt;/code&gt; dynamically imports &lt;code&gt;a1.js&lt;/code&gt; with &lt;code&gt;import(/* webpackChunkName: 'a1', webpackPreload: true */ &quot;./a1&quot;)&lt;/code&gt;; which means &lt;code&gt;a1.js&lt;/code&gt; will be preloaded&lt;/li&gt;
&lt;li&gt;all dynamic imports are conditional, e.g. the &lt;code&gt;import()&lt;/code&gt; function is called, for instance, on a button click; this is relevant because the dynamic chunks are not be loaded immediately&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Upon page load, only the &lt;code&gt;index&lt;/code&gt; chunk will be loaded.&lt;/p&gt;
&lt;p&gt;What would happen if the line that calls &lt;code&gt;import('b.js')&lt;/code&gt; is reached?&lt;br&gt;
Since &lt;code&gt;b&lt;/code&gt; is an async chunk, what happens is the &lt;code&gt;b.js&lt;/code&gt; file will be fetched over the network (e.g. through an HTTP request) and then it will be immediately executed.&lt;br&gt;
This means there is no preloading involved and, as a result, the user might need to wait for the network request to complete, as well for parsing and executing the file.&lt;br&gt;
&lt;strong&gt;However&lt;/strong&gt;, this does not mean the &lt;em&gt;preload&lt;/em&gt; resource hint should be overused - there could be downsides to it, too, such as cache pollution or redundantly used bandwidth.&lt;br&gt;
So, we should always choose our tools wisely!&lt;/p&gt;
&lt;p&gt;When importing &lt;code&gt;a.js&lt;/code&gt;, because it has instructed webpack to &lt;strong&gt;preload&lt;/strong&gt; &lt;code&gt;a1.js&lt;/code&gt;, the &lt;code&gt;a1&lt;/code&gt; chunk &lt;strong&gt;will also be fetched&lt;/strong&gt; over the network, but not executed. Instead, it will be stored in the browser's cache:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=17665989480005889993&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;a1.js&lt;/code&gt; file will be executed only when the line in &lt;code&gt;a.js&lt;/code&gt; that dynamically imports &lt;code&gt;a1.js&lt;/code&gt; is reached. Then, &lt;strong&gt;instead of making an HTTP request&lt;/strong&gt;, the &lt;code&gt;a1.js&lt;/code&gt; file will be retrieved right away from the cache and executed&lt;br&gt;
This it the beauty of preloading assets, in general.&lt;/p&gt;
&lt;p&gt;In this small example, we only focused on preloading JavaScript files, but the concept should apply for other resources too, such as fonts, images, videos, etc.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I walked through this example also in this &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.youtube.com/watch?v=RHZDvNyWa2Y&quot;&gt;YouTube video&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the next section, we will take a look at a slightly more complicated example of preloading, an example I have named, perhaps lacking inspiration, &lt;em&gt;nested preloading&lt;/em&gt;.&lt;/p&gt;
&lt;h3 id=&quot;nested-preloading&quot;&gt;Nested preloading&lt;/h3&gt;
&lt;p&gt;Here is an interesting and practical question worth investigating: &lt;em&gt;What happens if a preloaded chunk also preloads other chunks?&lt;/em&gt; &lt;/p&gt;
&lt;p&gt;Getting back to our diagram, there is just a simple addition: &lt;code&gt;a1.js&lt;/code&gt; dynamically imports &lt;code&gt;a2.js&lt;/code&gt; with the &lt;code&gt;webpackPreload: true&lt;/code&gt; magic comment:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=10790273431658908262&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;When will &lt;code&gt;a2.js&lt;/code&gt;'s chunk be fetched? Will it be fetched when the &lt;code&gt;a.js&lt;/code&gt; file is executed? Or maybe not when &lt;code&gt;a.js&lt;/code&gt; is executed?&lt;/p&gt;
&lt;p&gt;When this question came to mind, I immediately became very intrigued. So intrigued that I had to &lt;a rel=&quot;nofollow&quot; href=&quot;https://youtu.be/n5qT2Z-mrzY?si=JNtCkEnTQGaw-sId&quot;&gt;make a video&lt;/a&gt; about it where I explore my hypothesis and final result.&lt;/p&gt;
&lt;p&gt;Let's take a step-by-step approach to the questions above.&lt;br&gt;
Firstly and invariably, &lt;code&gt;index.js&lt;/code&gt; will be fetched and executed in the browser. This is because &lt;code&gt;index.js&lt;/code&gt; is an entry file (which makes the &lt;code&gt;index&lt;/code&gt; chunk an &lt;em&gt;entry chunk&lt;/em&gt;).&lt;br&gt;
Then, when &lt;code&gt;a.js&lt;/code&gt; is imported at a later time, because it preloads &lt;code&gt;a1.js&lt;/code&gt;, the latter will only be fetched by the browser, but not executed. The file will be executed when the line in &lt;code&gt;a.js&lt;/code&gt; that imports &lt;code&gt;a1.js&lt;/code&gt; is reached.&lt;/p&gt;
&lt;p&gt;However, what about the fact that &lt;code&gt;a1.js&lt;/code&gt; (the module that has just been fetched by the browser) &lt;strong&gt;also preloads&lt;/strong&gt; &lt;code&gt;a2.js&lt;/code&gt;? Will &lt;code&gt;a2.js&lt;/code&gt; be fetched now, too?&lt;/p&gt;
&lt;p&gt;The answer is &lt;strong&gt;no&lt;/strong&gt;. The explanation is straightforward: &lt;code&gt;a2.js&lt;/code&gt; can't be fetched because this only happens when &lt;code&gt;a1.js&lt;/code&gt; is executed by the browser, i.e. when it is actually required.&lt;br&gt;
But, at this point, &lt;code&gt;a1.js&lt;/code&gt; has &lt;strong&gt;only&lt;/strong&gt; been fetched and not yet required. So, since &lt;code&gt;a1.js&lt;/code&gt; is not executed -&amp;gt; &lt;code&gt;a2.js&lt;/code&gt; can't be fetched.&lt;/p&gt;
&lt;p&gt;What we have explored in this section was indeed not very complicated, but, in the next one, we will address a question that requires some under-the-hood knowledge of webpack. Let's see what that is about!&lt;/p&gt;
&lt;h3 id=&quot;not-all-chunks-can-be-preloaded&quot;&gt;Not all chunks can be preloaded&lt;/h3&gt;
&lt;p&gt;Although it may sound counter-intuitive, it is true - &lt;strong&gt;not all async chunks&lt;/strong&gt; can be preloaded.&lt;/p&gt;
&lt;p&gt;Coming back to our demo application, &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Andrei0872/understanding-webpack/blob/master/examples/chunk-preload/index.js#L1-L5&quot;&gt;let's try preloading &lt;code&gt;a.js&lt;/code&gt; from &lt;code&gt;index.js&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import(/* webpackChunkName: 'a', webpackPreload: true */ &quot;./a.js&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we reload the browser, we will see no HTTP request for the &lt;code&gt;a.js&lt;/code&gt; in the network tab, which means the file has not been preloaded.&lt;/p&gt;
&lt;p&gt;But, why is that? Why can't &lt;code&gt;a.js&lt;/code&gt; be preloaded?&lt;/p&gt;
&lt;p&gt;Let's bring back the diagram, with a small modification included:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=16492730467352985032&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;One thing that stands out from this diagram is that an &lt;em&gt;entry chunk&lt;/em&gt; dynamically imports an &lt;em&gt;async chunk&lt;/em&gt;.&lt;br&gt;
And this is, actually, the reason as to why the &lt;code&gt;a&lt;/code&gt; chunk can't be preloaded - its &lt;strong&gt;parent chunk&lt;/strong&gt; is an entry chunk.&lt;/p&gt;
&lt;p&gt;Now, another question emerges - why can't an entry chunk preload an async chunk?&lt;br&gt;
After some research, I have found &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jantimon/html-webpack-plugin/issues/1317#issuecomment-704870353&quot;&gt;this comment&lt;/a&gt; on an issue:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;When using preload you must use the script (call import()) within a few seconds [...] So only use preload when the imported module is mandatory for correct function of the app and the app is useless without it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To my understanding, if a chunk is &lt;strong&gt;critical to an entry chunk&lt;/strong&gt; (this is what is being indicated by using the &lt;em&gt;preload&lt;/em&gt; resource hint), then the async chunk can be statically imported. &lt;br&gt;
It it different if an async chunk needs has other async chunks that are critical for the functionality of the former, because one can't know for sure when the async chunk is actually loaded/needed.&lt;/p&gt;
&lt;p&gt;If you are curious to read the source code that is responsible for all of this magic, you can start from &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/webpack/webpack/blob/main/lib/prefetch/ChunkPrefetchPreloadPlugin.js#L46-L48&quot;&gt;here&lt;/a&gt;.&lt;br&gt;
I have also created a &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Andrei0872/understanding-webpack&quot;&gt;repo&lt;/a&gt; where you can find debugging instructions for exploring webpack via the debugger.&lt;/p&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;In this article, we have been reminded, first of all, of what &lt;em&gt;preloading&lt;/em&gt; means - it is a browser resource hint that allows us to fetch resources with priority, as soon as possible.&lt;/p&gt;
&lt;p&gt;Then, we have learned about what preloading really means in the context of webpack, the essential role of the &lt;code&gt;import()&lt;/code&gt; function, what &lt;em&gt;entry chunks&lt;/em&gt; are, what &lt;em&gt;async chunks&lt;/em&gt; are and how the ways these are connected influence the preloading behaviour.&lt;/p&gt;
&lt;p&gt;Thank you for reading!&lt;/p&gt;
</description>
<category>Web Dev</category>
<guid isPermaLink="true">https://coderlegion.com/1087/preloading-chunks-with-webpack</guid>
<pubDate>Wed, 05 Feb 2025 21:05:39 +0000</pubDate>
</item>
</channel>
</rss>