- Next.js 教程
- Next.js - 主页
- Next.js - 概述
- Next.js - 环境设置
- Next.js 特点
- Next.js - 页面
- Next.js - 静态文件服务
- Next.js - 元数据
- Next.js - CSS 支持
- Next.js - 全球 CSS 支持
- Next.js - 预渲染
- Next.js 路由
- Next.js - 路由
- Next.js - 动态 API 路由
- Next.js - 命令式路由
- Next.js - 浅层路由
- Next.js API 路由
- Next.js - API 路由
- Next.js - API 中间件
- Next.js - 响应助手
- Next.js 其他
- Next.js - 打字稿
- Next.js - 环境变量
- Next.js - 部署
- Next.js - CLI
- Next.js 有用资源
- Next.js - 快速指南
- Next.js - 有用的资源
- Next.js - 讨论
Next.js - 环境设置
由于 Next.js 是一个基于 React 的框架,因此我们使用 Node 环境。现在确保您的系统上安装了Node.js和npm。您可以使用以下命令来安装 Next.js -
npm install next react react-dom
Next.js 成功安装后,您可以观察到以下输出 -
+ react@16.13.1 + react-dom@16.13.1 + next@9.4.4 added 831 packages from 323 contributors and audited 834 packages in 172.989s
现在,让我们创建一个节点 package.json -
npm init
创建 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)
现在更新 package.json 的脚本部分以包含 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" }
创建页面目录。
在 nextjs 文件夹中创建一个 Pages 文件夹,并创建一个包含以下内容的 index.js 文件。
function HomePage() { return <div>Welcome to Next.js!</div> } export default HomePage
启动 Next.js 服务器
运行以下命令启动服务器 -。
npm run dev > nextjs@1.0.0 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
验证输出
在浏览器中打开 localhost:3000,您将看到以下输出。