开发Node.js项目时候,拷贝项目文件夹的时候发现竟然出现了Windows下文件名过长的问题。经过研究以后发现这个问题是项目目录当中的node_modules目录下组件间依赖关系导致的。
查看node_modules属性发现使用Yoeman创建的项目MyDemo初始就有大约80M,其中的依赖情况可以使用npm list查看
f:\MyDemo>npm list
情况大约是
[email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
│ │ │ └── [email protected]
│ │ └── [email protected]
....省略
结果就是依赖模块套依赖模块,有的模块依赖层次太深,因此项目目录出现文件名过长。
解决问题的思路参考
这里
- 使用npm 获取所有的依赖情况
- 检查模块的重复情况,需要带上版本号
- 使用npm install module@version --save-dev将重复的模块依赖更新到项目的package.json文件当中
- 删除项目当中的node_modules文件夹(我的例子f:\MyDemo\node_modules)
- 使用npm install重新安装node_modules目录
按这个思路处理完毕以后原来80多M的node_modules减肥为40多M,拷贝项目文件可以支持更深一点的目录了。
注意:处理一遍以后重复依赖大大减少,使用npm list 可以看出依然还有重复依赖。
我npm list 出来的信息有700多,咱不能用眼睛看吧?因此就有了下面的这个工具类:
1.使用npm list > c:/node_depend.txt
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* find duplicate modules in node.js projects
*
* <pre>
* 1. npm ls to get a list of all installed modules.
* 2.look through those modules and identify duplicate modules (version is important)
* 3.npm install module@version --save-dev to install those modules in the root node_modules
* directory and update package.json.
* 4.rmdir node_modules to delete the node_modules directory.
* 5.npm install to pull down a fresh copy of your dependencies.
* </pre>
*
* RefURL:http://stackoverflow.com/questions/13318364/how-to-deploy-node-js-
* application-with-deep-node-modules-structure-on-windows?rq=1
*
* @author [email protected]
*
*/
public class FindDupeModue {
public static void main(String[] args) {
String fileName = "c:/node_depend.txt";
List<String> moduleList = getModules(fileName);
List<String> dupeModue = checkDupeModue(moduleList);
printResult(dupeModue);
}
/**
* read modules from file
*
* @param fileName
* @return
*/
private static List<String> getModules(String fileName) {
List<String> list = new CopyOnWriteArrayList<String>();
BufferedReader br = null;
String s = null;
try {
br = new BufferedReader(new FileReader(new File(fileName)));
while ((s = br.readLine()) != null) {
list.add(format(s));
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return list;
}
/**
* format nodeName
*
* @param modulName
* @return
*/
private static String format(String modulName) {
String patternString = "[^A-Z|^a-z|^0-9|^.|^@|^-]";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(modulName);
return matcher.replaceAll("");
}
/**
* check out the DupeModue
*
* @param moduleList
* @return
*/
private static List<String> checkDupeModue(List<String> moduleList) {
System.out
.println("==========Begin Find DupeModule===================");
List<String> list = new ArrayList<String>();
for (String item : moduleList) {
// check in moduleList
List<String> allDupeList = getAllDupeModue(moduleList, item);
if (allDupeList.size() > 1) {
System.out.println(item + "=" + allDupeList.size());
list.add(item);
moduleList.removeAll(allDupeList);
} else {
moduleList.remove(item);
}
}
System.out.println("==========End Find DupeModule===================");
return list;
}
/**
* loop in moduleList
*
* @param moduleList
* @param item
* @return
*/
private static List<String> getAllDupeModue(List<String> moduleList,
String moduleName) {
List<String> tmpList = new ArrayList<String>();
for (String item : moduleList) {
if (moduleName.equals(item)) {
tmpList.add(item);
}
}
return tmpList;
}
/**
* printResult
* npm install module@version --save-dev
*
* @param moduleList
*/
private static void printResult(List<String> moduleList) {
System.out.println("=============Result===================");
for (String item : moduleList) {
System.out.println("npm install " + item + " --save-dev");
}
}
}
2.运行工具类看重复情况
==========Begin Find DupeModule===================
[email protected]=2
[email protected]=6
[email protected]=5
[email protected]=5
....(省略)
=============Result===================
npm install [email protected] --save-dev
npm install [email protected] --save-dev
npm install [email protected] --save-dev
npm install [email protected] --save-dev
npm install [email protected] --save-dev
...(省略)
Enjoy it!