Configure and install a modern frontend application.
Follow the steps below to ensure you are on the latest version of node and npm.
Download and install Nodejs: Nodejs website.
Install n to manage node versions. It needs to be installed globally.
npm install -g nInstall the LTS version of node.
n ltsRemove previously installed versions.
n pruneEnsure NPM is on the latest version available for you current Nodejs installation.
npm install -g npm@latestCorepack is included by default with all Node.js installs, but is currently opt-in. To enable it, run the following command.
corepack enableEnsure yarn on the latesd stable version.
corepack prepare yarn@stable --activateJust run the following command. It will generate some files inside your current directory; add them all to your next commit, and you'll be done!
yarn init -2Add webpack and webpack-cli on your project.
yarn add webpack webpack-cli --devCreate those new files. Get the content of the files on the project.
modern-web-app
...
+ |- index.html
+ |- /src
+ |- index.jsNow update the package.json file.
{
"name": "modern-web-app",
+ "private": true,
"packageManager": "yarn@3.6.1",
"devDependencies": {
"webpack": "^5.88.1",
"webpack-cli": "^5.1.4"
}
}Let's get back to index.js file, extracted this quote from the original documentation so feel free to explore it:
In this example, there are implicit dependencies between the <script> tags. Our index.js file depends on lodash being included in the page before it runs. This is because index.js never explicitly declared a need for lodash; it assumes that the global variable _ exists.
There are problems with managing JavaScript projects this way:
It is not immediately apparent that the script depends on an external library. If a dependency is missing, or included in the wrong order, the application will not function properly. If a dependency is included but not used, the browser will be forced to download unnecessary code. Let's use webpack to manage these scripts instead.