Next.js: configuración del entorno
Como Next.js es un marco basado en reacciones, estamos utilizando el entorno Node. Ahora asegúrate de tenerNode.js y npminstalado en su sistema. Puede usar el siguiente comando para instalar Next.js:
npm install next react react-dom
Puede observar el siguiente resultado una vez que Next.js se haya instalado correctamente:
+ [email protected]
+ [email protected]
+ [email protected]
added 831 packages from 323 contributors and audited 834 packages in 172.989s
Ahora, creemos un paquete de nodo.json -
npm init
Seleccione los valores predeterminados al crear un package.json -
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (nextjs)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to \Node\nextjs\package.json:
{
"name": "nextjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"next": "^9.4.4",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
Ahora actualice la sección de scripts de package.json para incluir comandos de Next.js.
{
"name": "nextjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"next": "^9.4.4",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "next",
"build": "next build",
"start": "next start"
},
"author": "",
"license": "ISC"
}
Crear directorio de páginas.
Cree una carpeta de páginas dentro de la carpeta nextjs y cree un archivo index.js con el siguiente contenido.
function HomePage() {
return <div>Welcome to Next.js!</div>
}
export default HomePage
Inicie el servidor Next.js
Ejecute el siguiente comando para iniciar el servidor:
npm run dev
> [email protected] dev \Node\nextjs
> next
ready - started server on http://localhost:3000
event - compiled successfully
event - build page: /
wait - compiling...
event - compiled successfully
event - build page: /next/dist/pages/_error
wait - compiling...
event - compiled successfully
Verificar salida
Abra localhost: 3000 en un navegador y verá el siguiente resultado.