为什么 npm start(在 create-react-app 之后)失败 运行?

Why is npm start (after create-react-app) failing to run?

在我使用 create-react-app 创建示例 React 应用程序后,npm 启动失败。

我已经使用节点 11.4 和 10.4 (npm 6.4.1) 进行了尝试

npm install create-react-app -g
create-react-app t2
cd t2
npm start

预计

能够在 localhost:3000

启动简单的反应服务器以在浏览器中命中

实际

服务器没有启动,在我的终端得到这个:

$ npm start

> t2@0.1.0 start /Users/samr/dev/githubs/react/t2
> react-scripts start

/Users/samr/dev/githubs/react/t2/node_modules/ajv/lib/keyword.js:65
      throw new Error('custom keyword definition is invalid: '  + this.errorsText(validateDefinition.errors));
      ^

Error: custom keyword definition is invalid: data.errors should be boolean
    at Ajv.addKeyword (/Users/samr/dev/githubs/react/t2/node_modules/ajv/lib/keyword.js:65:13)
    at module.exports (/Users/samr/dev/githubs/react/t2/node_modules/ajv-errors/index.js:10:7)
    at Object.<anonymous> (/Users/samr/dev/githubs/react/t2/node_modules/schema-utils/src/validateOptions.js:22:1)
    at Module._compile (internal/modules/cjs/loader.js:723:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:659:17)
    at require (internal/modules/cjs/helpers.js:22:18)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! t2@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the t2@0.1.0 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!     /Users/samr/.npm/_logs/2019-02-09T23_19_40_848Z-debug.log

只需将 resolutions 添加到您的 package.json 中,如下所示:

  { 
    "dependencies": {
      ...
    }
    "resolutions": {
      "ajv": "6.8.1"
    }
  }

然后 运行 npm install。如果你使用纱线,yarn install

这里提到了快速修复:

基本上转到node_modules/ajv/lib/keyword.js并注释掉第64和65行。

根据 Github 回答中的建议,以下更改使我能够工作。

To fix this error, just change:
Line 14: `errors: 'full',`
to:
Line 14: `errors: true`
in file **node_modules\ajv-errors\index.js**

正如@atsnam 所说,您可以在此处找到解决方案 https://github.com/webpack/webpack/issues/8768

如果你使用纱线

在下面添加到 package.json

"resolutions": {
  "ajv": "6.8.1"
}

然后 运行 yarn install

如果你使用 npm

npm uninstall ajv
npm install ajv@6.8.1

对我有用

已接受的答案对我不起作用。相反,我这样做了:

  • 已删除 node_modules 文件夹。
  • cd 到项目父目录。

    npm 安装
    npm 开始

这很有效。