解决'npm WARN saveError ENOENT:没有这样的文件或目录,请打开'/Users/<username>/package.json''错误 [英] Solving the 'npm WARN saveError ENOENT: no such file or directory, open '/Users/<username>/package.json'' error

查看:620
本文介绍了解决'npm WARN saveError ENOENT:没有这样的文件或目录,请打开'/Users/<username>/package.json''错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,因此请提供指向URL的链接或解释术语,以便我理解.

我设法通过终端在Mac OS(10.13.3)上安装了'npm',并使用它安装了一些软件包,例如SASS.

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.

我现在正尝试使用npm安装 sass-mq .我认为我已经成功安装了它,但是我想对我可能做的事情有不完整或做错的事情发表第二意见.

I'm now trying to install sass-mq using npm. 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.

最初,我按照sass-mq Github页面上的说明进行操作:

npm install sass-mq --save

这给了我这个错误:

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

环顾四周,我意识到我打算使用

npm init

..在键入我的"install 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?**

我想这是说我不能使用'sass-mq'(这是程序包的名称,作为我要在本地计算机上安装的本地程序包的名称(?). href ="https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm" rel ="noreferrer">此处有更多信息.

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.

所以我只是尝试了这个:

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:

它似乎工作正常.

我的问题是:这是我应该做的正确方法吗?您通常如何做到这一点?

我也收到这些警告-可以忽略它们吗?

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:您执行此操作的方式很好,您不必担心这些警告.

TL;DR: The way you have done it is fine, and you needn't worry about those warnings.

要进一步了解npm为何存在及其运行方式,请继续阅读.

For a more in-depth idea of why npm exists and how it works, read on.

npm代表节点软件包管理器.软件包是节点生态系统的基本组成部分-它们的存在是为了让您可以使用其他人的解决方案来解决常见问题.

npm stands for Node Package Manager. 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可帮助您以更易于管理和思考的方式处理这些依赖项",但是使用npm并非必需-您可以编写一个节点应用程序,在其中自己组织所有这些不同的文件.但是,这将很快变得非常混乱,因此至少在99%的情况下并没有真正的优势.还有其他的软件包管理者-我个人使用yarn,但是他们都在尝试做类似的事情,因此选择主要是优先考虑的问题,不在本讨论的范围之内.

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. That's going to get very confusing, very quickly, however, so there's no real advantage in at least 99% of cases. 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.

因此,当您开始一个新项目时,请键入npm init,这将告诉npm在名为package.json的文件夹中创建一个文件,该文件将帮助您组织这些依赖项. package.json将保存有关您自己的应用程序的信息(本身就是一个程序包),以及您告诉npm您将在您自己的项目中将其用作依赖项的程序包的信息.这就是为什么它会问您所有关于软件包名称和描述的问题,以便一旦您发布它,人们就会知道与谁联系,它做什么,它是什么版本等.

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. 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. 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.

首先,将创建您的项目目录.这只是一个空的新目录.我们称之为ts.在ts目录中,在终端中键入npm init并回答其问题(尽管我通常只按回车键即可),然后在目录中查找,您将看到package.json文件.打开文件,您将看到所有您的软件包信息.目前就是这样.

First you will make your project directory. This is just an empty new directory. Let's call it 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. Open the file, and you will see all your package information. And currently that's it.

现在返回命令行并键入npm install time-stamp.完成思考后,再次打开package.json,您将在依赖项"列表中看到引用的时间戳. (从npm 5开始,不再需要使用--save选项.npm现在将其作为默认值.

So now back to the command line and type 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.' (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?)

在目录内部,您还将看到另一个名为package-lock.json的文件和一个名为node_modules的目录.

Back inside the directory, you will also see another file called package-lock.json and a directory called node_modules.

node_modules目录将包含一个名为时间戳的目录,其中包含使时间戳生效的所有代码.您可能不需要经常查看这里,但是可以,如果您查看时间戳目录,您会发现它具有自己的package.json!打开它,看看,它本身具有安装所需的所有信息.您会注意到它没有任何依赖关系,但是如果有,它们将与所有 他们 依赖关系一起安装在您的node_modules中. 他们的 依赖性...和 他们的... 如果您希望看到此效果,请尝试安装测试框架开玩笑.同样,只需npm install jest.

The node_modules directory will contain a directory called time-stamp and that holds all the code that makes time-stamp work. 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! 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. Again, just npm install jest.

希望整个事情现在开始变得有意义了……

Hopefully the whole thing's beginning to make a bit of sense, now...

锁定文件稍微复杂一些.它的作用是确保在将项目部署到新系统时使用完全相同的依赖关系集.它之所以需要这样做,是因为npm组织事物的方式可能取决于最新的发行版等,如果您尝试部署应用程序会很烦人,但由于您的依赖项表现出不同的行为而无法正常工作.开发环境中的方法!

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.

一旦安装了依赖项,就可以在项目中的任何地方 requireimport,而不必担心将其定向到目录结构中的正确路径.只需require('time-stamp'),它将正常工作!

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. Just require('time-stamp') and it will work just fine!

最后,为了达到目的,做得很好,值得一提的是全局安装.使用-g选项(即npm install time-stamp -g)意味着依赖项将安装在计算机上某个中央的node_modules目录中,而不是安装在项目的node_modules文件夹中.但是,您仍然需要将其链接到您的项目(这样它最终会作为package.json中的依赖项),您可以通过键入npm link 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. 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. 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.

这一切都意味着package.json和锁文件的组合完美表示了node_modules中所有文件,这意味着您 不需要它们在您的git(或其他存储库)中. 接着.由于您的依赖项中涉及所有文件,因此在您拥有大型项目时,这变得尤为重要,但是从第一天开始,将'node_modules'添加到您的.gitignore是一个好习惯.但是我开始摆脱话题,所以也许我应该在这里结束...

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. 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. 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:没有这样的文件或目录,请打开'/Users/&lt;username&gt;/package.json''错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆