简体   繁体   English

npm-将已安装的软件包另存为依赖项

[英]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 wrote a module called pkg-save . 我编写了一个名为pkg-save的模块。
You can have a try if your npm version is "2.xx". 如果您的npm版本为“ 2.xx”,则可以尝试一下。
I haven't test in npm v3, so I don't know whether it is useful or not in npm v3. 我尚未在npm v3中进行测试,所以我不知道它在npm v3中是否有用。

I faced this issue when I cloned a new project from bitbucket. 当我从bitbucket克隆一个新项目时遇到了这个问题。 I solved this by the following steps: 我通过以下步骤解决了这个问题:

  1. Go to root folder where package.json exists in your project terminal. 转到项目终端中package.json存在的根文件夹
  2. Then run the following command. 然后运行以下命令。

$ npm install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM