首页 > 解决方案 > 带有 bable-node 的 nodemon 在终端崩溃但服务器运行良好?

问题描述

我正在尝试将 nodemon 与 bable-node 一起使用。我的 package.json 中有这个命令:

"open-graph-playground": "nodemon --exec babel-node src/graphql/mock-server.js",

这是 JavaScript 文件:

import fs from 'fs';
import open from 'open';
import { buildSchema } from 'graphql';
import express from 'express';
import graphqlHTTP from 'express-graphql';

import root from './root';

const schemaString = fs.readFileSync(`${__dirname}/schema.graphql`, 'utf8');
const app = express();
const schema = buildSchema(schemaString);

app.use(
  '/mock-graphql-playground',
  graphqlHTTP({
    schema,
    rootValue: root,
    graphiql: true,
  }),
);

app.listen(4001);

open('http://localhost:4001/mock-graphql-playground');

当我yarn open-graph-playground在终端中运行时,我收到此错误:

yarn run v1.13.0
$ nodemon --exec babel-node src/graphql/mock-server.js
[nodemon] 1.18.10
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node src/graphql/mock-server.js`
2019-05-30 15:45 node[1683] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
2019-05-30 15:45 node[1683] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
[nodemon] Internal watch failed: EMFILE: too many open files, watch
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

奇怪的是,即使终端中的进程已关闭,服务器仍在运行http://localhost:4001/mock-graphql-playground

标签: nodemonbabel-node

解决方案


您可能已经达到操作系统允许的文件监视的软限制。但是服务器运行良好,因为文件监视不是服务器的组成部分。文件监视用于在您更改源代码时重新加载服务器。发生这种情况的原因有两个。

1.Nodemon正在监视项目外的文件

由于您的文件夹结构,nodemon 可能会查看额外的文件,例如您的node_modules文件夹。通过使用将 nodemon 指向正确的目录--watch ./src

2.您的项目包含的文件多于观察限制

某些操作系统对一次可以查看的文件数量有限制。您的项目或您的环境可能需要观看比系统允许的更多的文件。在 linux 上更改它是直截了当的

sysctl -n -w fs.inotify.max_user_watches=16384

更多信息和来源


推荐阅读