如何在没有Now的情况下在本地构建NextJS项目

JavaScript React.js

乐米亚

2020-03-24

我需要在本地构建我的React NextJS项目以将其托管在Appache服务器上,当我运行命令时run build它不会生成构建文件夹。

关于它的IR&D,每个人都推荐,ZEIT – Next.js build with Now但是它在云上构建项目,但是我需要为本地appache服务器构建。所以请帮帮我。

这是我的package.json:

......
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "next start"
},
......

第3295篇《如何在没有Now的情况下在本地构建NextJS项目》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
猴子村村 2020.03.24

经过如此多的努力,我找到了问题的答案,我必须在package.json中的脚本下添加以下行:

"scripts": {
    ......
    "export": "npm run build && next export"
    .....
},

基本上,我的情况npm run build && next export是完成NextJS项目所需的完整命令。因此,将其添加到package.json后,您只需要在terminal:中运行 npm export ,它将为nextjs项目生成完整的构建。

小小 2020.03.24

您有一个build运行的package.json脚本next buildnext build默认情况下,命令将构建用于开发的应用程序,但不会创建生产捆绑包。

为了在本地计算机上创建生产捆绑包,您需要指定您处于生产环境中NODE_ENV=production(此操作在现在和其他部署服务器中自动完成)。基本上,您的package.json最终将:

"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "next start",
"prod:build": "NODE_ENV=production npm run build"
},

如果愿意,可以直接替换npm run buildnext build

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android