在 create-react-app 创建后立即添加包不起作用

Adding a package immediately after created by create-react-app is not working

我对反应很陌生(一天前开始)。我使用 create-react-app 命令行创建了一个应用程序。我按以下顺序试了

此时应用 运行 正常。然后我做了以下

现在我收到这个错误

my-app@0.1.0 start /Users/shanmugharajk/Code/udemy/my-app react-scripts start

sh: react-scripts: command not found npm ERR! file sh npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn npm ERR! my-app@0.1.0 start: react-scripts start npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the my-app@0.1.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

每次我尝试安装任何软件包时都会发生这种情况。

我想我注意到的是当我 运行

npm WARN registry Using stale data from https://registry.npmjs.org/ because the host is inaccessible -- are you offline? npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning ENOTFOUND: request to https://registry.npmjs.org/redux failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443 npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation. npm WARN gentlyRm not removing /Users/shanmugharajk/Code/udemy/my-app/node_modules/html-minifier/node_modules/.bin/uglifyjs as it wasn't installed by /Users/shanmugharajk/Code/udemy/my-app/node_modules/html-minifier/node_modules/uglify-js npm WARN gentlyRm not removing /Users/shanmugharajk/Code/udemy/my-app/node_modules/espree/node_modules/.bin/acorn as it wasn't installed by /Users/shanmugharajk/Code/udemy/my-app/node_modules/espree/node_modules/acorn npm WARN gentlyRm not removing /Users/shanmugharajk/Code/udemy/my-app/node_modules/autoprefixer/node_modules/.bin/browserslist as it wasn't installed by /Users/shanmugharajk/Code/udemy/my-app/node_modules/autoprefixer/node_modules/browserslist npm notice created a lockfile as package-lock.json. You should commit this file.

  • redux@3.7.1 added 3 packages, removed 1142 packages and updated 3 packages in 27.043s

我想不通原因。请帮助我。

您正在使用 npm 5。目前它有很多问题。

我建议降级到 npm 4 并重试:

npm install -g npm@4

rm -rf node_modules
rm package-lock.json
npm install

如果没有帮助,请检查您的互联网连接。 这看起来像是您的网络问题:

getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443

我得到了答案,因为我的机器上安装了 yarn 和 npm,create-react-app 使用 yarn 并安装所有依赖项并创建 yarn.lock 文件。

所以现在当我 运行 npm install 它寻找 package.lock.json 但它不会在那里。所以它会在创建项目时卸载由 yarn 创建的一些包。

所以我找到的解决方案是执行以下任一操作

  • create-react-app my-app
  • npm install
  • Then do install npm install -package-

  • create-react-app my-app
  • yarn install -package-

这两种方法现在对我都有效。

运行 React 应用程序的基本要求是:

 nodejs  
 npm

如果未安装,请通过(专门针对 Ubuntu)

安装
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

创建 React js 应用程序:

npx create-react-app my-DemoApp

一个 之后进入文件夹:

cd my-DemoApp

您可以看到由此创建的基本文件夹结构

my-DemoApp
├── README.md
├── package.json
├── .gitignore
├── public
│   ├── favicon.ico
│   ├── index.html
│   └── manifest.json
└── src
    ├── App.css
    ├── App.js
    ├── App.test.js
    ├── index.css
    ├── index.js
    ├── logo.svg
    └── serviceWorker.js

在 运行ning 添加 React 应用所需的基本依赖之后:

npm install

这将在您的父文件夹中创建名为 node_modules 的新文件夹

您现在可以通过以下方式 运行 您的应用:

npm start

现在您已完成最基本的设置,您的应用程序现已准备就绪。

您还可以通过 npm

添加更多应用程序必需的库

npm 安装-g npm

然后再次安装create react app:

npm install -g create-react-app

然后重新创建项目