有什么区别:
npm install [package_name]
and:
npm install [package_name] --save
and:
npm install [package_name] --save-dev
这是什么意思?——save和-dev关键字的真正作用是什么?
有什么区别:
npm install [package_name]
and:
npm install [package_name] --save
and:
npm install [package_name] --save-dev
这是什么意思?——save和-dev关键字的真正作用是什么?
当前回答
——save-dev用于应用程序开发中使用的模块,而不是在生产环境中运行时需要的模块 ——save用于将其添加到包中。Json,它是运行应用程序所必需的。
例如:express,body-parser,lodash,helmet,mysql所有这些都是在运行应用程序时使用的,而mocha,istanbul,chai,sonarqube-scanner都是在开发过程中使用的,所以把它们放在dev-dependencies中。
NPM link或NPM install也会在你的项目文件夹中安装开发依赖模块和依赖模块
其他回答
基本上我们写
npm install package_name
但特别为了测试目的,我们不需要在应用程序运行在正常状态下运行一些包,因此Node引入了很好的方法来解决这个问题。每当我们写作时
npm install package_name --save-dev
当时,该包仅用于开发目的而安装。
——save-dev用于应用程序开发中使用的模块,而不是在生产环境中运行时需要的模块 ——save用于将其添加到包中。Json,它是运行应用程序所必需的。
例如:express,body-parser,lodash,helmet,mysql所有这些都是在运行应用程序时使用的,而mocha,istanbul,chai,sonarqube-scanner都是在开发过程中使用的,所以把它们放在dev-dependencies中。
NPM link或NPM install也会在你的项目文件夹中安装开发依赖模块和依赖模块
as——save是npm的默认选项,所以我使用
npm i package
还有,我用的是
npm i package -D
默认选项将安装包作为项目依赖项,其中as -D为开发依赖项,如测试,lint等,并为开发过程安装包
你可以在这里找到所有的标志https://docs.npmjs.com/cli/v8/commands/npm-install
这里所有的解释都很棒,但缺少一个非常重要的东西:如何仅安装生产依赖项?(没有开发依赖项)。 我们通过使用——save或——save-dev将依赖项与devDependencies分开。 安装所有我们使用的:
npm i
要只安装生产包,我们应该使用:
npm i --only=production
阅读完成并忘记——保存开发头痛
最简单的答案是——当你为其他开发人员创建包,并希望将包托管在NPM Registry(如lodash、mongoose、express等)时,save-dev非常有用。当你构建或编写Node Server时,——save和——save-dev之间没有区别,因为你的Node Server实现对你来说是私有的,你永远不会在NPM上发布它。
NPM安装如何工作
Whenever we install a new package using npm like npm install express then NPM installs that package to our system and put it into node_modules folder, now NPM will analyze the package.json file of newly installed package i.e express in this case, after analyzing NPM will install all those packages which were mentioned in dependencies section of package.json file of express package. After installing those packages on which express was dependent NPM again analyze the package.json file of all newly installed packages and again install the packages for them, this cycle goes on until all packages are available into node_modules folder to function properly. You can check package dependencies by running npm list in terminal where terminal should point location of your project directory.
——save-dev如何与上述解释的内容相关
Suppose you want to create a new package like express, now while development of this new package you probably want to write some unit testing code and test the package with any other available testing package let's assume mocha in this case. Now you know mocha is only required to test the package not required to use the package. In this case you should install mocha using --save-dev flag, otherwise NPM will install it whenever a developer install your package using NPM. So if we want a dependency not installed when someone install our package from NPM we must install that package using --save-dev in development phase.
最后一件事
不要把——save-dev和协作开发混在一起,如果有人从像github这样的源版本控制系统中克隆了你的包代码,那么NPM肯定会安装所有的devDependencies,也就是使用——save-dev安装的包。