NPM package.json文件的主要用途是什么? [英] What are the main uses for the NPM package.json file?

查看:117
本文介绍了NPM package.json文件的主要用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此处 package.json文件中的依赖项允许人们通过npm-安装项目时安装依赖项.

I read from here that the dependencies in the package.json file allow people to install the dependencies if they install your project through npm-

最后,依赖项字段用于列出所有依赖项 在npm上可用的项目.当有人安装您的 项目通过npm,列出的所有依赖项将安装为 出色地.此外,如果有人运行npm,请在根目录中安装 项目,它将所有依赖项安装到 ./node_modules.

Finally, the dependencies field is used to list all the dependencies of your project that are available on npm. When someone installs your project through npm, all the dependencies listed will be installed as well. Additionally, if someone runs npm install in the root directory of your project, it will install all the dependencies to ./node_modules.

如果某人未在您项目的根目录中运行npm install,则将所有依赖项安装到何处?

Where will all the dependencies be installed to if someone doesn't run npm install in the root directory of your project?

此外,如果他们选择通过Github克隆此项目怎么办?无论如何,它准备就绪,对吗?那么,除了向用户提供有关项目的元数据之外,package.json文件的目的是什么?

Also, what if they choose to clone this project through Github instead? It would be ready to go anyway, right? Then at that point what is the purpose of the package.json file besides giving the user meta data about the project?

推荐答案

如果某人没有在您项目的根目录中运行npm install,那么将所有依赖项安装到哪里?

Where will all the dependencies be installed to if someone doesn't run npm install in the root directory of your project?

如果您的意思是如果在其他目录中运行命令,它们将安装在哪里",NPM将向上搜索父目录,直到找到package.json,然后将依赖项安装在node_modules中该文件旁边的文件夹. IE.它们将始终以项目根结尾.

If by that you mean 'where will they be installed if you run the command in a different directory', NPM will search upwards through the parent directories until it finds package.json, and then install the dependencies in a node_modules folder next to that file. I.E. they'll always end up in the project root.

此外,如果他们选择通过Github克隆此项目怎么办?无论如何,它准备就绪,对吗?那么,除了向用户提供有关项目的元数据之外,package.json文件的目的是什么?

Also, what if they choose to clone this project through Github instead? It would be ready to go anyway, right? Then at that point what is the purpose of the package.json file besides giving the user meta data about the project?

不是这种情况!节点项目几乎总是有一个.gitignore文件,该文件显式地将node_modules排除在版本控制之外,并希望您在下载源代码后运行npm install.

This isn't the case! Node projects just about always have a .gitignore file which explicitly excludes node_modules from being committed to version control, and expect you to run npm install after downloading the source.

在项目库中遵守语义版本控制的理由很少,可以在GitHub存储库中拥有依赖项(绝大多数软件包都这样做),npm install绝不会导致下载不兼容的版本,并且如果您绝对需要锁定依赖项的版本,则只需使用

There's very few good reasons to have your dependencies in your GitHub repository - as long as a project adheres to Semantic Versioning (the vast majority of packages do), npm install will never cause incompatible versions to be downloaded, and if you absolutely need to lock down the versions of your dependencies, you can just use npm shrinkwrap.

编辑:正如Matt的评论非常有帮助地指出的那样,NPM的一些功能超越了简单的元数据-我可能最常使用的功能是

As Matt's comment very helpfully pointed out, there's several features of NPM that go beyond simple metadata - the one I probably get the most use out of is Scripts, which allow you to create project-specific aliases for command-line operations.

运行Webpack开发服务器的一个例子对我来说很方便-它在devDependencies中本地安装到我的项目中(安装软件包时可以使用--save-dev选项进行操作),因此如果是手动操作,则必须输入以下内容:

An example of where this has come in handy for me is running the Webpack development server - it's installed locally to my project in the devDependencies (which you can do using the --save-dev option when installing a package), so if I was doing it manually, I would have to type something along the lines of:

"./node_modules/.bin/webpack-dev-server" --inline --hot

坦率地说,这会有些痛苦.相反,我可以将其添加到package.json中(请注意,使用NPM脚本时,node_modules/.bin会自动添加到系统路径中,因此您无需每次都键入该内容):

Which quite frankly, would be a bit of a pain. Instead, I can just add this to my package.json (note that node_modules/.bin is automatically added to the system path when using an NPM script, so you don't need to type that every time):

"scripts": {
    "dev": "webpack-dev-server --inline --hot"
}

然后我要做的就是:

npm run dev

除了这个简单的用例之外,还有一些特殊"脚本名称会在某些事件中自动调用-例如,prepublish在将程序包发布到注册表之前运行.

Beyond this simple use-case, there's also several 'special' script names which are automatically called upon certain events - for example, prepublish is run before publishing a package to the registry.

这篇关于NPM package.json文件的主要用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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