简体   繁体   English

安装 react 时 npm 找不到 package.json

[英]npm cant find package.json when installing react

This is a followup question to my earlier question (but is an independent question)这是我之前问题的后续问题(但这是一个独立问题)

I am trying to install react and react-dom on a Mac:我正在尝试在 Mac 上安装 react 和 react-dom:

npm install --save react react-dom

but get the following warnings (path names replaced with ... ):但得到以下警告(路径名替换为... ):

npm WARN saveError ENOENT: no such file or directory, open '/Users/../Z/package.json'
/Users/.../Z
├─┬ react@15.4.1 
│ ├─┬ fbjs@0.8.8 
│ │ ├── core-js@1.2.7 
│ │ ├─┬ isomorphic-fetch@2.2.1 
│ │ │ ├─┬ node-fetch@1.6.3 
│ │ │ │ ├─┬ encoding@0.1.12 
│ │ │ │ │ └── iconv-lite@0.4.15 
│ │ │ │ └── is-stream@1.1.0 
│ │ │ └── whatwg-fetch@2.0.1 
│ │ ├─┬ promise@7.1.1 
│ │ │ └── asap@2.0.5 
│ │ ├── setimmediate@1.0.5 
│ │ └── ua-parser-js@0.7.12 
│ ├─┬ loose-envify@1.3.0 
│ │ └── js-tokens@2.0.0 
│ └── object-assign@4.1.0 
└── react-dom@15.4.1 

npm WARN enoent ENOENT: no such file or directory, open '/Users/.../Z/package.json'
npm WARN Z No description
npm WARN Z No repository field.
npm WARN Z No README data
npm WARN Z No license field.

I searched around a bit but did not find a good reason for these warnings.我搜索了一下,但没有找到这些警告的充分理由。 SO posts like these seem to suggest installing react in the same directory where node modules are installed.这样的帖子似乎建议在安装节点模块的同一目录中安装 react。 My node is installed in usr/local/bin but package.json does not appear anywhere on the machine even on a global search.我的节点安装在usr/local/bin但即使在全局搜索中, package.json也不会出现在机器上的任何地方。 I just installed npm before trying to install react so don't think its a versioning issue.我只是在尝试安装 react 之前安装了 npm,所以不要认为这是一个版本问题。

If this is a brand new project, it can be resolved by running npm init from the root directory of your project (where you want package.json to be created), and press "ENTER" at all of the prompts to accept the default answers.如果这是一个全新的项目,可以通过从项目的根目录(要创建package.json )运行npm init来解决,并在所有提示处按“ENTER”接受默认答案. (It is easy to change your responses by directly modifying the package.json file later.) (稍后通过直接修改package.json文件可以轻松更改您的响应。)

Once the package.json has been created, you will be able to run npm install commands!创建package.json ,您将能够运行npm install命令!

Actually for me it just worked like this : I ran npx create-react-app "nameOfProject" , if you type npm start in the same folder it won't work because it tries to find the package.json in the same folder , when your package.json is in the subfolder ( the folder of the "nameOfProject" ).实际上对我来说它只是这样工作的:我运行 npx create-react-app "nameOfProject" ,如果你在同一个文件夹中输入 npm start 它将不起作用,因为它试图在同一个文件夹中找到 package.json ,当您的 package.json 位于子文件夹(“nameOfProject”的文件夹)中。 Run cd "nameOfProject" to change the directory and them run npm start .运行 cd "nameOfProject" 以更改目录,然后运行 ​​npm start 。

Check the folder you're in. Usually after using npx create-react-app, you'll be in the same folder.检查你所在的文件夹。通常在使用 npx create-react-app 后,你会在同一个文件夹中。 you will be needed to cd into the new folder that is created after running above command.您将需要 cd 进入运行上述命令后创建的新文件夹。 there you'll find package.json.在那里你会找到 package.json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM