简体   繁体   English

解决&#39;npm WARN saveError ENOENT: no such file or directory, open &#39;/Users/<username> /package.json&#39;&#39; 错误

[英]Solving the 'npm WARN saveError ENOENT: no such file or directory, open '/Users/<username>/package.json'' error

I'm a newbie so please include links to URLs or explain terminologies so I can understand.我是新手,所以请包含指向 URL 的链接或解释术语,以便我理解。

I've managed to install 'npm' on a Mac OS (10.13.3) via the terminal, and have installed some packages like SASS using it.我设法通过终端在 Mac OS (10.13.3) 上安装了“npm”,并使用它安装了一些软件包,如 SASS。

I'm now trying to install sass-mq using npm.我现在正在尝试使用 npm 安装sass-mq I think I've managed to install it, but I'd like a second opinion on what I might have done that was incomplete, or wrong while doing it.我想我已经设法安装了它,但我想就我可能做的不完整或做错的事情提出第二意见。

Initially, following the instructions on the sass-mq Github page, I was trying to use:最初,按照 sass-mq Github 页面上的说明,我尝试使用:

npm install sass-mq --save

which gave me this error:这给了我这个错误:

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

    + sass-mq@5.0.0
    updated 1 package and audited 1 package in 1.67s
    found 0 vulnerabilities

Looking around, I realised I'm meant to be using环顾四周,我意识到我打算使用

npm init

..before typing my 'install sass-mq --save' command. ..在输入我的“安装 sass-mq --save”命令之前。

Cool, done that.酷,做到了。 Next error was this:下一个错误是这样的:

package name: (nikhil) sass-mq
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/nikhil/package.json:

{
  "name": "sass-mq",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "sass-mq": "^5.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) 
darwin:~ nikhil$ npm install sass-mq --save
npm ERR! code ENOSELF
npm ERR! Refusing to install package with name "sass-mq" under a package
**npm ERR! also called "sass-mq". Did you name your project the same
npm ERR! as the dependency you're installing?**

I think this is saying that I can't use 'sass-mq' (which is the name of the package, as the name of the local package (?) I'm installing into on my local machine. Some more info here.我认为这是说我不能使用“sass-mq”(这是包的名称,作为本地包的名称(?)我正在本地机器上安装。 这里有更多信息。

So I simply tried this:所以我只是尝试了这个:

**package name: (sass-mq) media-queries-nikhil**
version: (1.0.0) 
description: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/nikhil/package.json:

and it seems to have worked OK.它似乎工作正常。

My question is: Is this the right way I should have done this?我的问题是:这是我应该这样做的正确方式吗? How do you usually do this?你通常如何做到这一点?

Also, I get these warnings - is it OK to ignore them?另外,我收到这些警告 - 可以忽略它们吗?

npm WARN media-queries-nikhil@1.0.0 No description
npm WARN media-queries-nikhil@1.0.0 No repository field.

I'm creating this question in part so others like me looking for the answer to a similar issue can find an explanation, instead of just commands they need to fix their issue.我创建这个问题的部分原因是像我这样寻找类似问题答案的人可以找到解释,而不仅仅是他们需要解决问题的命令。 I found a few similar question-threads, but none that actually explained what was happening and why.我发现了一些类似的问题线程,但没有一个能真正解释发生了什么以及为什么。

Thanks for reading, I really appreciate any help with this :)感谢您的阅读,我非常感谢您对此的任何帮助:)

TL;DR: The way you have done it is fine, and you needn't worry about those warnings. TL;DR:您这样做的方式很好,您不必担心这些警告。

For a more in-depth idea of why npm exists and how it works, read on.要更深入地了解 npm 存在的原因及其工作原理,请继续阅读。

npm stands for Node Package Manager. npm 代表节点包管理器。 Packages are a fundamental part of the node ecosystem - they exist to allow you to use other people's solutions to common problems.包是节点生态系统的基本组成部分——它们的存在是为了让您可以使用其他人的解决方案来解决常见问题。

However, this can get very confusing, since, because this is an open source community, they are all being released at different times by different people.然而,这可能会变得非常混乱,因为这是一个开源社区,它们都是由不同的人在不同的时间发布的。 Also, two different packages that you use may actually be dependent on a third package that is completely unknown to you, and potentially they may even need different versions of that package.此外,您使用的两个不同的包实际上可能依赖于您完全不知道的第三个包,并且可能它们甚至可能需要该包的不同版本。

As you can already see, this has the potential to get very messy.正如您已经看到的,这有可能变得非常混乱。

npm helps you deal with these 'dependencies' in a way that is easier to manage and think about, however it is not essential to use npm - you can write a node app where you organise all these different files yourself. npm 可以帮助您以更易于管理和思考的方式处理这些“依赖项”,但是使用 npm 并不是必需的 - 您可以编写一个节点应用程序,您可以自己组织所有这些不同的文件。 That's going to get very confusing, very quickly, however, so there's no real advantage in at least 99% of cases.然而,这会很快变得非常混乱,因此至少在 99% 的情况下没有真正的优势。 There are also other package managers - personally I use yarn but they're all trying to do similar things, so that choice is mainly a matter of preference and outside the scope of this discussion.还有其他包管理器——我个人使用纱线,但他们都在尝试做类似的事情,所以这个选择主要是一个偏好问题,不在本讨论的范围内。

So when you start a new project, you type npm init and this tells npm to make a file in your folder called package.json that is going to help you organise these dependencies.所以当你开始一个新项目时,你输入npm init这会告诉 npm 在你的文件夹中创建一个名为 package.json 的文件,它会帮助你组织这些依赖项。 package.json will hold the information about your own app (which is a package in its own right) and also which packages you have told npm you are going to be using as dependencies in your own project. package.json 将保存有关您自己的应用程序(它本身就是一个包)的信息,以及您告诉 npm 将在您自己的项目中用作依赖项的哪些包。 This is why it asks you all those questions about your package name and description, so that if you ever publish it, people will know who to contact, what it does, what version it is, etc.这就是为什么它会问你关于你的包名称和描述的所有问题,这样如果你发布它,人们就会知道联系谁,它做什么,它是什么版本等。

It is only important to give this serious thought if you actually intend to publish your package, which is less likely in the case of a website, but very likely if you're making a library.如果您确实打算发布您的软件包,那么认真考虑这一点很重要,这在网站的情况下不太可能,但如果您正在制作图书馆,则很有可能。 However, as you've already discovered, packages are meant to have unique names, which is why you should call your package something personal to you, so you don't end up with a naming conflict like you did when you tried to name your package the same as a package you were later going to try to install.但是,正如您已经发现的那样,包意味着具有唯一的名称,这就是为什么您应该将您的包称为您个人的东西,这样您就不会像尝试命名您的包时那样出现命名冲突包与您稍后要尝试安装的包相同。

So let's create our own package, and install our first dependency (which, remember, is just another package).所以让我们创建我们自己的包,并安装我们的第一个依赖项(记住,它只是另一个包)。 I'm going to choose time-stamp as a dependency because it's nice and small.我将选择时间戳作为依赖项,因为它既好又小。

First you will make your project directory.首先,您将创建项目目录。 This is just an empty new directory.这只是一个空的新目录。 Let's call it ts .我们称之为ts From inside the ts directory, type npm init into your terminal and answer its questions (although I usually just press return to them all,) then look inside the directory and you will see the package.json file.在 ts 目录中,在终端中输入npm init并回答它的问题(虽然我通常只按回车键),然后查看目录内部,您将看到 package.json 文件。 Open the file, and you will see all your package information.打开该文件,你会看到所有的软件包信息。 And currently that's it.目前就是这样。

So now back to the command line and type npm install time-stamp .所以现在回到命令行并输入npm install time-stamp When it's finished thinking, open package.json again and you will see time-stamp referenced in the list of 'dependencies.'当它完成思考后,再次打开 package.json,您将看到“依赖项”列表中引用的时间戳。 (As of npm 5 it is no longer necessary to use the --save option. npm now assumes this as default. What is the --save option for npm install? ) (从 npm 5 开始,不再需要使用--save选项。npm 现在假定这是默认值。npm install 的 --save 选项是什么?

Back inside the directory, you will also see another file called package-lock.json and a directory called node_modules.回到目录内部,您还将看到另一个名为 package-lock.json 的文件和一个名为 node_modules 的目录。

The node_modules directory will contain a directory called time-stamp and that holds all the code that makes time-stamp work. node_modules 目录将包含一个名为 time-stamp 的目录,其中包含使 time-stamp 工作的所有代码。 You probably don't need to look in here very often, but you can, and if you look inside the time-stamp directory you will see it has got its own package.json!你可能不需要经常查看这里,但你可以,如果你查看时间戳目录,你会看到它有自己的 package.json! Open it up and have a look, and there's all the information it needs to install itself.打开它看看,里面有它自己安装所需的所有信息。 You'll note that it doesn't have any dependencies, but if it did, they would be installed in your node_modules with all of their dependencies as well... and their dependencies... and theirs... If you want to see this in action, try installing the testing framework 'jest.您会注意到它没有任何依赖项,但如果有,它们将连同它们的所有依赖项一起安装在您的 node_modules 中......以及它们的依赖项......以及它们的......如果你想看到这个在行动,尝试安装测试框架'jest。 Again, just npm install jest .同样,只需npm install jest

Hopefully the whole thing's beginning to make a bit of sense, now...希望整件事开始变得有意义,现在......

The lock file is slightly more complicated.锁定文件稍微复杂一些。 What it does is make sure that when you deploy your project onto a new system that you use exactly the same set of dependencies.它的作用是确保在将项目部署到新系统时使用完全相同的依赖项集。 It needs to do this because the way npm organises things can be dependent on latest release versions, etc, and it would be very annoying if you were to try to deploy your app and it didn't work because your dependencies were behaving in a different way from your development environment!它需要这样做,因为 npm 组织事物的方式可能取决于最新的发布版本等,如果您尝试部署您的应用程序并且它不起作用,这将是非常烦人的,因为您的依赖项的行为方式不同方式从您的开发环境!

Having said all this, basically you can ignore it at this stage!说了这么多,基本上现阶段可以无视了! It's an important part of npm, but you shouldn't edit it directly unless you really know what you're doing.它是 npm 的重要组成部分,但除非您真的知道自己在做什么,否则不应直接编辑它。

Once you have installed your dependency, you will be able to require or import it anywhere in your project, without having to worry about directing it to the correct path in your directory structure.一旦安装了依赖项,您就可以在项目的任何位置requireimport它,而不必担心将其定向到目录结构中的正确路径。 Just require('time-stamp') and it will work just fine!只需require('time-stamp')就可以了!

Finally, and well done for getting this far, it's worth mentioning global installation.最后,为了做到这一点做得很好,值得一提的是全局安装。 Using the -g option - that is npm install time-stamp -g - means that the dependency will be installed in a central node_modules directory somewhere on your computer rather than in your project's node_modules folder.使用-g选项 - 即npm install time-stamp -g - 意味着依赖项将安装在您计算机上某处的中央 node_modules 目录中,而不是您项目的 node_modules 文件夹中。 However, you will still need to link it to your project (so that it ends up as a dependency in your package.json) and you would do that by typing npm link time-stamp .但是,您仍然需要将它链接到您的项目(以便它最终成为您的 package.json 中的依赖项),您可以通过键入npm link time-stamp Personally I like all my modules to be local to my project, but again this depends on your use-cases and to an extent personal preference.就我个人而言,我喜欢我的所有模块都在我的项目中,但这同样取决于您的用例和个人偏好。

What this all means is that the combination of your package.json and the lock file is a perfect representation of all the files in your node_modules, and this means that you don't need to have them in your git (or other repository.) You can pull your repository down to a new server, and all you have to do is type npm install and they'll be dragged down from the internet there and then.这一切意味着你的 package.json 和锁定文件的组合是 node_modules 中所有文件的完美代表,这意味着你不需要在你的 git(或其他存储库)中拥有它们。你可以将你的存储库拉到一个新的服务器上,你所要做的就是输入npm install然后它们就会从互联网上拖下来。 This becomes much more important when you have a large project, because of all the files involved in your dependencies, but it's a good habit to add 'node_modules' to your .gitignore from day one.当您有一个大型项目时,这变得更加重要,因为您的依赖项中涉及所有文件,但是从第一天起将“node_modules”添加到您的 .gitignore 是一个好习惯。 But I'm starting to get off topic so maybe I should end here...但我开始跑题了,所以也许我应该在这里结束......

This is only intended to be a basic introduction, so I have kept it fairly simple, and I don't want to create an in-depth tutorial, but if you need clarification on any of the points I've made, feel free to comment and I'll make edits if I can!这只是一个基本的介绍,所以我保持相当简单,我不想创建一个深入的教程,但如果你需要澄清我提出的任何观点,请随时评论,如果可以,我会进行编辑!

npm WARN saveError ENOENT: no such file or directory, open '/Users/{username}/package.json' npm WARN saveError ENOENT: 没有这样的文件或目录,打开'/Users/{username}/package.json'

  • You don't have a package.json > use npm init你没有 package.json > 使用npm init

  • You are in the wrong directory > cd to the folder where your package.json is, like so:您在错误的目录中 > cd到您的 package.json 所在的文件夹,如下所示:

    cd C://Dev/MySolution/MyWebProject and then try again. cd C://Dev/MySolution/MyWebProject然后再试一次。


npm WARN media-queries-nikhil@1.0.0 No description npm WARN media-queries-nikhil@1.0.0 无说明

npm WARN media-queries-nikhil@1.0.0 No repository field. npm WARN media-queries-nikhil@1.0.0 没有存储库字段。

Abulifa's answer explains that your project could be published as it's own npm package... In that scenario, these warnings would help warn that your package.json is missing some fields. Abulifa 的回答解释了您的项目可以作为它自己的 npm 包发布...在这种情况下,这些警告将有助于警告您的 package.json 缺少某些字段。

If you know you'll never publish as an npm package, and want to hide these warnings, add this to your package.json:如果您知道您永远不会发布为 npm 包,并且想要隐藏这些警告,请将其添加到您的 package.json 中:

"description": "filling out this field to avoid warnings",
"repository": "not publishing",
"readme": "not publishing",
"license": "not publishing",

Run the following:运行以下命令:

npm init -y

That will create the package.json file which you can edit later with proper information.这将创建package.json文件,您可以稍后使用适当的信息对其进行编辑。

Reference: https://www.digitalocean.com/community/questions/npm-warn-saveerror-enoent-package-json-not-found参考: https : //www.digitalocean.com/community/questions/npm-warn-saveerror-enoent-package-json-not-found

Issue:问题:

npm install fails with below error npm install失败并出现以下错误
npm WARN saveError ENOENT: no such file or directory, open '.../package.json'' error

Cause原因

npm install will need package.json in the current directory you are in, which is missing. npm install将需要您所在的current directory package.json ,而该current directory缺失。

Solution解决方案

Assume there are 2 directories:假设有2个目录:

C:\dir1_p\       <--------- package.json exists
C:\dir2\         <--------- package.json does not exist

cd C:\dir1_p\
npm intall      <---------- PASS, since package.json is present

cd C:\dir2\
npm intall      <---------- ERROR, since package.json is Not present  (this was my problem)

So, are you in the correct directory when you did npm install ?那么,当您执行npm install时,您是否在正确的目录中?

Case 1: if you are in wrong directory, cd to a directory where package.json exists, then run npm install案例1:如果你在错误的目录中, cdpackage.json所在的目录,然后运行npm install

Case 2: if you are in correct directory and you want to create a new package.json , then run npm init and press ENTER keys until it is completed, this will create package.json in current directory, now run npm install情况2:如果你在正确的目录中并且你想create a new package.json ,然后运行npm init并按ENTER键直到它完成,这将在当前目录中创建package.json ,现在运行npm install

Hope this helps someone.希望这可以帮助某人。

The same issue occurred for me when running:运行时我也遇到了同样的问题:

npm install -g @vue/cli

After much research and experimentation the only thing that worked instead was:经过大量研究和实验,唯一有效的方法是:

npm install -g @vue/cli@latest

You can't name your project equal the package name that you are trying to install.您不能将您的项目命名为您尝试安装的包名称。

Rename your project at the package json and try again or try npm init again and use another name.在包 json 中重命名您的项目并重试或再次尝试npm init并使用另一个名称。

主要问题之一可能是您不在根文件中,如果您应该运行 npm install,请确保您在客户端文件夹或服务器端,而不是在某些随机文件或没有 package.json 的父文件中。其中的 json

关闭项目并重新打开它,这将解决问题

暂无
暂无

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

相关问题 npm卸载本地软件包会引发错误“ saveError ENOENT:无此类文件或目录,请打开&#39;/Users/vikramanna/package.json&#39;” - npm uninstall for local package is throwing error “saveError ENOENT: no such file or directory, open '/Users/vikramanna/package.json'” npm WARN saveError ENOENT:无此类文件或目录,请打开“ /app/package.json” - npm WARN saveError ENOENT: no such file or directory, open '/app/package.json' npm WARN enoent ENOENT:没有这样的文件或目录,打开“C:\Users\Nuwanst\package.json” - npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Nuwanst\package.json' npm ERR! enoent ENOENT:没有这样的文件或目录,请打开package.json - npm ERR! enoent ENOENT: no such file or directory, open package.json npm ENOENT 没有这样的文件或目录,打开 {path}/package.json - npm ENOENT no such file or directory, open {path}/package.json npm-bundle 失败并出现错误:ENOENT:没有这样的文件或目录,打开“package.json” - npm-bundle fails with Error: ENOENT: no such file or directory, open 'package.json' npm 错误! enoent ENOENT: 没有那个文件或目录,打开&#39;/app/package.json&#39; - npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json' npm WARN saveError EPERM: 不允许操作,重命名 &#39;C:\\...\\package.json.3542172463&#39; -&gt; &#39;C:\\...\\package.json&#39; - npm WARN saveError EPERM: operation not permitted, rename 'C:\...\package.json.3542172463' -> 'C:\...\package.json' Docker - npm ERR:enoent ENOENT,没有这样的文件或目录。 打开 '/package.json' ----- /docker-entrypoint:sh: 38: exec: npm: not found - Docker - npm ERR! enoent ENOENT: no such file or directory, open '/package.json' ----- /docker-entrypoint.sh: 38: exec: npm: not found npm ENOENT:没有这样的文件或目录,打开“/usr/app/package.json”。 在尝试使用 docker-compose 与 docker 共享本地空间时 - npm ENOENT: no such file or directory, open '/usr/app/package.json'. While trying to share local space with docker using docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM