2

所以我在 babel 编译所有东西的地方工作,包括 next.js

我按照 next.js 文档了解如何处理 babel 并创建了一个 .babelrc 文件:

{
   "presets": ["next/babel", "es2015", "stage-0"]
}

当我运行yarn run dev一切编译和服务器启动。当我加载页面时,next.js 将运行它的构建过程。由于目录中的内容发生了变化,nodemon 会重新启动服务器并导致无限循环。请问有人可以帮我吗???

这是我的 package.json 文件:

{
  "name": "creatorsneverdie",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "dev": "nodemon app.js --exec babel-node --ignore node_modules, .next, yarn.lock",
    "build": "next build",
    "start": "NODE_ENV=production node app.js"
  },
  "dependencies": {
    "axios": "^0.16.2",
    "bcrypt": "^1.0.2",
    "body-parser": "^1.17.2",
    "cors": "^2.8.4",
    "cryptr": "^2.0.0",
    "dotenv": "^4.0.0",
    "express": "^4.15.3",
    "express-session": "^1.15.4",
    "lodash": "^4.17.4",
    "lowdb": "^0.16.2",
    "next": "^2.4.7",
    "passport": "^0.3.2",
    "passport-jwt": "^2.2.1",
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-loader": "^7.1.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "nodemon": "^1.11.0"
  }
}

和 app.js 文件:

import express from 'express'
import session from 'express-session'
import bodyParser from 'body-parser'
import cors from 'cors'
import passport from 'passport'


const db = require('./db/index.js').initDb()
const writeSeeds = require('./db/index.js').writeSeeds

const routes = require('./routes/index')

require('dotenv').config({path: 'variables.env'});


// Next config
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const nextLoader = next({dev})
const handle = nextLoader.getRequestHandler()

nextLoader.prepare().then(() => {
    const app = express();

    app.use(cors());
    app.set('db', db);
    app.nextRender = nextLoader

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));

    app.use(session({
        secret: process.env.SECRET,
        resave: false,
        saveUninitialized: false
    }));

    app.use(passport.initialize());
    app.use(passport.session());
    require('./config/passport')(passport);

    app.use('/', routes)

    app.get('*', (req, res) => {
        return handle(req, res)
    })

    // START APP
    app.set('port', process.env.PORT || 1337);

    if(!db.has('products').value()) {
        writeSeeds(db);
    }
    const server = app.listen(app.get('port'), () => {
        console.log(`Express running -> ${server.address().port}`)
    });



})
4

2 回答 2

0

我遇到了同样的问题,我通过将“pages/ ”* 添加到 nodemon 的忽略列表来解决这个问题。不知何故,构建过程正在重写页面源代码而不更改任何内容,这会导致 nodemon 重新启动并再次触发重建。

于 2017-09-12T10:24:07.997 回答
0

尝试将您的开发脚本更改为:

nodemon -w app.js --exec babel-node
于 2018-05-14T19:45:09.363 回答