Starting from this chapter, we will enter the practical tutorial section. In the practical tutorial, we will start with environment preparation, starting from simple to complex, building a real project step by step.
Before getting started, you will need to install Node.js, and ensure that your Node.js version is higher than 18.20.8. We recommend using the LTS version of Node.js 22.
You can check the currently used Node.js version with the following command:
If you do not have Node.js installed in your current environment, or the installed version is lower than 18.20.8, you can use nvm or fnm to install the required version.
Here is an example of how to install the Node.js 22 LTS version via nvm:
Both nvm and fnm are Node.js version management tools. Relatively speaking, nvm is more mature and stable, while fnm is implemented using Rust, which provides better performance than nvm.
Additionally, after installing nvm or fnm, when there is a .nvmrc
file containing lts/jod
in the repository's root directory, the system will automatically install or switch to the correct Node.js version upon entering the repository.
Modern.js currently still supports Node 16, but will officially terminate support for Node.js 16 in June 2025. To ensure a smooth transition for your project, please complete the Node.js version upgrade before June 2025.
It is recommended to use pnpm to manage dependencies:
Modern.js also supports dependency management with yarn
and npm
.
We create a new directory and initialize the project via the command line tool:
@modern-js/create
provides an interactive Q & A interface to initialize the project based on the results, with initialization performed according to the default settings:
After create the project, Modern.js will automatically install dependencies and create a git repository.
Now, the project structure is as follows:
Run pnpm run dev
in the project to start the project:
Open http://localhost:8000/
in your browser to see the page content.
We delete the original sample code and replace it with a simple point of contact list:
Remove redundant css files and keep the directory free of redundant files:
Since the framework supports HMR by default, you can see that the content in http://localhost:8080/ is automatically updated to:
The page has no styles at the moment. The next chapter will expand on this section.
Next, we modify the modern.config.ts
in the project to enable the SSR capability:
Re-execute pnpm run dev
to find that the project has completed page rendering at the server level.