[英]npm - save installed packages as dependencies
I've installed some packages via npm install $package
, without setting up a package.json
first. 我已经通过
npm install $package
安装了一些软件包,而没有先设置package.json
。 Now I would like to create a package.json
file, but keep all installed packages as dependencies. 现在,我想创建一个
package.json
文件,但将所有已安装的软件包保留为依赖项。 Simply running npm init
doesn't offer this option, can I achieve this automatically? 简单地运行
npm init
不提供此选项,我可以自动实现吗?
Update January 2016 2016年1月更新
npm now supports this out of the box. npm现在开箱即用。 I have npm version 3.5.2 .
我有npm 3.5.2版本。
so with just a node_modules folder with underscore installed. 因此仅安装了下划线的node_modules文件夹。
npm init --yes
then: 然后:
cat package.json
Contained within package.json: 包含在package.json中:
"dependencies": {
"underscore": "^1.8.3"
},
UPDATE: With the launch of npm v3, this trick will create a lot of unwanted entries on your package.json
file. 更新:随着npm v3的启动,此技巧将在
package.json
文件上创建许多不需要的条目。 That's because module dependencies are now flattened, as @sunny-mittal pointed out. 因为@ sunny-mittal指出,现在模块依赖关系已经变平。
npm
doesn't support that, as far as I know. 据我所知,
npm
不支持该功能。 You'd have do reinstall each package passing --save
to each one. 您必须重新安装每个传递
--save
软件包。
But, there's a workaround, if your're on Unix based systems. 但是,如果您使用的是基于Unix的系统,则有一种解决方法。 From inside your project root folder, with a
package.json
file already created ( npm init
, as you mentioned), run: 在您的项目根文件夹中,已创建一个
package.json
文件( npm init
,如上所述),运行:
npm install $(ls node_modules/) --save
and it will reinstall the packages, and save them into package.json
as dependencies
. 它将重新安装这些软件包,并将它们作为
dependencies
保存到package.json
中。
Since NPM node_modules
is flat now and @Rodrigo's answere don't deal to well with that. 由于NPM
node_modules
现在很平坦,而node_modules
对此并node_modules
。
This is what I knitted together. 这就是我编织在一起的东西。
npm list --depth=0 | sed "1d" | sed -E "s/^[\`+-]+\s//" | sed -E "s/@(.*)$//"
This is essentially what ls node_modules
did before. 这实质上是
ls node_modules
之前所做的。
One-liner to save installed. 一线保存安装。
npm install $(npm ls | sed "1d" | sed -E "s/^[\`+-]+\s//" | sed -E "s/@(.*)$//") --save
I'm using 我正在使用
$ npm --version
3.5.3
Which lists like this. 像这样的清单。
$ npm list --depth=0
x@0.1.0 /home/victor/x
+-- babel-eslint@5.0.0-beta6
+-- babel-preset-es2015@6.3.13
+-- gulp@3.9.0
+-- gulp-babel@6.1.1
`-- gulp-eslint@1.1.1
I faced this issue when I cloned a new project from bitbucket. 当我从bitbucket克隆一个新项目时遇到了这个问题。 I solved this by the following steps:
我通过以下步骤解决了这个问题:
$ npm install
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.