带有Express的NextJS:将文件复制到服务器并运行“ npm run start”失败

问题描述 投票:0回答:1

我正在创建一个NextJS应用程序,我想将相关文件复制到我的服务器上并运行它。

为了在服务器上获取应用程序,我执行以下操作

  1. 在本地开发应用程序
  2. 使用“ npm run dev在本地测试”
  3. 使用“ npm run build”然后“ npm run start”在本地测试
  4. 应用程序运行得很好
  5. 将文件移动到服务器位置并进行部署
    1. cp .next〜/ myTestFolder
    2. cp package.json〜/ myTestFolder
    3. cp package-lock.json〜/ myTestFolder
    4. cp next.config.js〜/ myTestFolder
    5. cd〜/ myTestFolder
    6. npm安装
    7. npm运行构建
    8. npm运行开始
    9. 应用程序运行得很好

我使用Express后,此过程将不再起作用。我需要使用express和dotenv,以便为我的应用程序提供可自定义的basePath,该基础路径在将应用程序部署到的不同服务器上可能会有所不同(请参阅其他堆栈溢出问题:NextJS deploy to a specific URL path

我做了以下事情:

安装新的依赖项

npm install --save express dotenv

创建“ .env”文件

BASE_PATH=/my-test-application/path-for-my-app

更新“ next.config.js”以从“ .env”读取变量并将其传递给WebPack,以便可以在“ .js”文件中使用它

// support getting variables from the server environment
const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack')

module.exports = {
    // Stop being being able to access pages without using proper URL
    useFileSystemPublicRoutes: false,

    webpack: function(cfg) {
        // pass the environment variables to webpack so JS files can access them
        cfg.plugins.push(new webpack.EnvironmentPlugin(localEnv))    
        ....
    }
    ....
 }

更新“ package.json中的脚本”

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

创建“ ssr-server.js”文件进行路由

const express = require('express')
const next = require('next')

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  const server = express()

  server.get('/my-test-application/path-for-my-app', (req, res) => {
    return app.render(req, res, '/index', req.query)
  })

  server.get('/my-test-application/path-for-my-app/page1', (req, res) => {
    return app.render(req, res, '/page1', req.query)
  })

  server.get('/posts/:id', (req, res) => {
    return app.render(req, res, '/posts', { id: req.params.id })
  })

  server.all('*', (req, res) => {
    return handle(req, res)
  })

  server.listen(port, err => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  })
})

更新任何以使用此路径

render(){
    let basePath = process.env.BASE_PATH? process.env.BASE_PATH: "";
    ...
    <Link href={`${basePath}/page1`}> 
    ...

我可以使用“ npm run dev”和“ npm run build” /“ npm run start”在本地运行应用程序。

但是,当我将文件复制到服务器位置时,它现在失败。我复制了与上述相同的所有文件,并添加了2个新文件

  • cp ssr-server.js〜/ myTestFolder
  • cp .env〜/ myTestFolder

我运行“ npm build”,但是当我运行“ npm run start”时,失败,并显示以下内容

$ npm run start

> [email protected] start /home/myTestFolder
> node ssr-server.js

/home/myTestFolder/node_modules/next/dist/lib/find-pages-dir.js:3
if(existsSync(_path.default.join(dir,'..','pages'))){throw new Error('> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?');}throw new Error("> Couldn't find a `pages` directory. Please create one under the project root");}
                                                                                                                                                                   ^
Error: > Couldn't find a `pages` directory. Please create one under the project root
    at findPagesDir (/home/myTestFolder/node_modules/next/dist/lib/find-pages-dir.js:3:170)
    at new DevServer (/home/myTestFolder/node_modules/next/dist/server/next-dev-server.js:1:3830)
    at createServer (/home/myTestFolder/node_modules/next/dist/server/next.js:2:105)
    at Object.<anonymous> (/home/myTestFolder/ssr-server.js:5:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ssr-server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/myTestFolder/.npm/_logs/2020-03-06T10_24_04_105Z-debug.log

知道我想念/做错了什么吗?

reactjs express next.js dotenv
1个回答
0
投票

错误清楚地表明:

[Error: > Couldn't find adirectory.

您也必须​​cp pages ~/myTestFolder

© www.soinside.com 2019 - 2024. All rights reserved.