下一个J&通天塔导致无限循环

时间:2017-07-26 19:59:28

标签: javascript node.js babeljs nextjs

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

我关注了如何处理babel的next.js文档并创建了一个.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}`)
    });



})

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

nodemon -w app.js --exec babel-node