Javascript 没有 package.json 的 Node.js 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33518128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js project with no package.json
提问by user781486
Is it ok to have a node.js project with no package.json? The ones I see on the internet all come with package.json
有一个没有 package.json 的 node.js 项目可以吗?我在网上看到的都是 package.json 自带的
What is the effect of having no package.json?
没有 package.json 有什么影响?
How is package.json created in the first place? Is it created automatically? I am wondering why I do not have package.json
package.json 首先是如何创建的?它是自动创建的吗?我想知道为什么我没有 package.json
回答by AdityaParab
Fundamentally, package.json
is a meta file for your application. It lists all the configuration of your application.
从根本上说,package.json
是您的应用程序的元文件。它列出了应用程序的所有配置。
What is the effect of having no package.json?
没有 package.json 有什么影响?
Nothing as far as you're running all your code locally and have no requirement for deployment whatsoever.
只要您在本地运行所有代码并且对部署没有任何要求,就什么都没有。
Let's setup a scene for you to understand this better.
Imagine that you wrote a brilliant application using node. Now all the chicks in your surrounding want it to play with. It is so fantastic!
Now you want to give it to them and during the development process you npm install
ed so many things that your project grows beyond 4TB size.
让我们设置一个场景让您更好地理解这一点。想象一下,您使用 node 编写了一个出色的应用程序。现在你周围的所有小鸡都想和它一起玩。太棒了!现在你想把它给他们,在开发过程中你npm install
做了很多事情,你的项目增长到超过 4TB 大小。
There is no data storage device available to shit that huge code base.
没有可用的数据存储设备来处理庞大的代码库。
Then the girl of your dream said I want it and I want it now. So you begin searching for app deployment process for node applications.
然后你梦寐以求的女孩说我想要,我现在就想要。因此,您开始搜索节点应用程序的应用程序部署过程。
That is where you stumble upon a magical thing called package.json
.
在那里你偶然发现了一个叫做package.json
.
So what you do is you list all your npm install
ed modules under dependencies
property. Then you delete node_modules
folder, add package.json
and commit the entire damn thing in github. Even the .zip
file is of 10MB
因此,您要做的是npm install
在dependencies
属性下列出所有ed 模块。然后你删除node_modules
文件夹,package.json
在github中添加并提交整个该死的东西。即使.zip
文件是10MB
Then she gets the code.
Types in npm install && npm start
(which will install all the dependencies from the package.json` and start your application)
然后她得到了密码。输入npm install && npm start
(这将安装 package.json` 中的所有依赖项并启动您的应用程序)
If you have package.json however, that is where you specify all your dependencies.
但是,如果您有 package.json,则可以在此处指定所有依赖项。
Using --save
flag of npm install
使用--save
标志npm install
Example.
例子。
npm install express --save
npm install express --save
How is package.json created in the first place? Is it created automatically?
package.json 首先是如何创建的?它是自动创建的吗?
You can manually create a text file and save it as package.json
您可以手动创建一个文本文件并将其另存为 package.json
OR
或者
A more sophisticated way is to use the command
更复杂的方法是使用命令
npm init
npm init
I am wondering why I do not have package.json
我想知道为什么我没有 package.json
Me too! :)
我也是!:)
You're most probably following a tutorial that doesn't emphasize on initial configuration of the project OR the author of those tutorials presume that the reader has all the fundamentals down to begin with.
您很可能正在学习一个不强调项目初始配置的教程,或者这些教程的作者假定读者已经掌握了所有基础知识。
回答by XCS
It is created automatically if you write npm init
.
如果您编写npm init
.
Then, every package you add using npm install packagename --save
will be added to the dependencies list.
然后,您添加的每个包npm install packagename --save
都将添加到依赖项列表中。
You need package.json
so that when you want to use your project on another machine you don't have to copy all node_modules
, but only your .js
files you have written, assets and package.json
. You can then run npm install
command and it will automatically download and install all the required modules (found in the list of dependencies inside package.json
).
你需要package.json
这样,当你想用你的项目另一台机器上,你不必复制所有node_modules
,但只有你的.js
文件,你写,资产和package.json
。然后你可以运行npm install
命令,它会自动下载并安装所有需要的模块(在里面的依赖列表中找到package.json
)。
You can also manually create or edit it, but it's easier to add --save
when installing a module so you don't have to worry about package versions and stuff like that.
您也可以手动创建或编辑它,但--save
在安装模块时添加更容易,因此您不必担心包版本和类似的东西。
Also if you want to create a npm package, an open source project or stuff other people will use, it's either required or the norm to have this package.json
file describing your project.
此外,如果您想创建一个 npm 包、一个开源项目或其他人将使用的东西,那么使用此package.json
文件来描述您的项目是必需的或规范的。
回答by marcin93w
package.json is npmfile, if you don't use npm you will not have this file, npm is a great tool if you want to use external libraries in your project but if you don't need it (which is very not likely unless you are doing something very simple), you don't need package.json file too.
package.json 是npm文件,如果你不使用 npm 你就没有这个文件,如果你想在你的项目中使用外部库,npm 是一个很好的工具,但如果你不需要它(这不太可能)除非您正在做一些非常简单的事情),否则您也不需要 package.json 文件。
To generate package.json file initialize npm in your project using npm init
要生成 package.json 文件,请使用在您的项目中初始化 npm npm init