开发Node.js项目时候,拷贝项目文件夹的时候发现竟然出现了Windows下文件名过长的问题。经过研究以后发现这个问题是项目目录当中的node_modules目录下组件间依赖关系导致的。
查看node_modules属性发现使用Yoeman创建的项目MyDemo初始就有大约80M,其中的依赖情况可以使用npm list查看
f:\MyDemo>npm list
情况大约是
MyDemo@0.0.0
├─┬ grunt@0.4.4
│ ├── async@0.1.22
│ ├── coffee-script@1.3.3
│ ├── colors@0.6.2
│ ├── dateformat@1.0.2-1.2.3
│ ├── eventemitter2@0.4.13
│ ├── exit@0.1.2
│ ├─┬ findup-sync@0.1.3
│ │ ├─┬ glob@3.2.9
│ │ │ └── inherits@2.0.1
│ │ └── lodash@2.4.1
....省略
结果就是依赖模块套依赖模块,有的模块依赖层次太深,因此项目目录出现文件名过长。
解决问题的思路参考
这里
- 使用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 tobato@msn.com
*
*/
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===================
underscore@1.4.4=2
inherits@2.0.1=6
chalk@0.4.0=5
ansi-styles@1.0.0=5
....(省略)
=============Result===================
npm install underscore@1.4.4 --save-dev
npm install inherits@2.0.1 --save-dev
npm install chalk@0.4.0 --save-dev
npm install ansi-styles@1.0.0 --save-dev
npm install delayed-stream@0.0.5 --save-dev
...(省略)
Enjoy it!
分享到:
相关推荐
代码都已安排注释,下载后先安装node_modules依赖, main.js中配置好密钥 components=>map=>index.vue中也需要密钥 才可正常运行 因为这项目是很久之前的,经过高德地图api更新之后,作者并未进行修改。代码可自行...
本文介绍了webpack4.0打包优化策略整理小结,分享给大家,具体如下: webapck4 新特性介绍-参考资料 当前依赖包的版本 1.优化loader配置 1.1 缩小文件匹配范围... exclude: /node_modules/, // 排除不处理的目录
查看了下项目的package.json、package-lock.json文件,该模块和依赖模块的信息并没有改变,node_modules/a中的版本信息也和package.json中的对应。 一下子没了头绪,只好到node_modules中去调试一下。 TL;DR; 拉到...
"func": "node_modules.bearcat-dao.lib.connection.cache.redisConnectionManager", "props": [{ "name": "port", "value": "${redis.port}" }, { "name": "host", "value": "${redis....
通过尝试从package-lock.json或yarn.lock文件中获取所有必要的信息来优化此过程,并且只有在不存在锁定文件的情况下,才node_modules/**的完全扫描恢复为完整扫描。用法打印此模块的依赖关系树的认证数据: const ...
npm link 操作不够友好,相同依赖需要额外进行打包配置,多个 node_modules 管理不方便,依赖版本的控制也较为繁琐。 lerna Lerna 是一种工具针对使用 git 和 npm 管理多软件包代码仓库的工作流程进行优化。 lerna ...
用于React Native的Matomo SDK 本文档介绍了如何开始使用Matomo Tracking SDK for React Native。 是领先的开源Web分析平台,可为您提供有关网站访问者,营销... 将node_modules/react-native-matomo/ios/BNFMatomo.xc
您可以查看v2.0.0-删除了jQuery依赖性,对图标使用了Font Awesome v3.0.0-优化的版本,删除了第三方字体提供程序,添加了更多动画npm install module-notificationyarn add module-notification requirejs define ...
使用Yarn Workspaces将软件包提升到根node_modules/目录 安装 创建一个新的无服务器项目 $ git clone https://github.com/AnomalyInnovations/serverless-lerna-yarn-starter my-project 输入新目录 $ cd my-...
项目发起人 Starter Gulp + Browserify + less 项目以及如何完成一些常见任务和工作流程的示例。 基于 。 ... ./node_modules/.bin/gulp 安装 npm 依赖 npm install 这会遍历package.json列出的所
入门模板如果您不熟悉 Gulp、Browserify 或... 您可能需要使用sudo npm install -g gulp或者,您可以运行项目本地安装的 gulp 版本,而不是 ./node_modules/.bin/gulp安装 npm 依赖 npm install这会遍历package.json
yarn 在node_modules文件夹中安装该项目的依赖node_modules 。发展yarn start 在开发模式下运行应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒错误。测验...
是一种PNG优化器,可以将图像文件重新压缩为较小的大小,而不会丢失任何信息 您可能需要 。 安装 $ npm install --save optipng-bin 用法 const { promisify } = require ( 'util' ) ; const { execFile } = ...
bower_components-> bower.json->前端node_modules-> package.json->后端 源目录 src-> .jade css-> .styl jade-> .jade [includes] js-> .js或.coffee src->源代码src_files->静态资产 输出目录 开发->人类友好的...
删除node_modules目录,然后重新安装package.json中的所有内容: make clean make deps 构建应用的优化版本以进行分发: make dist 实际发生了什么? node和npm二进制文件现在在容器中运行,但是其他没有改变。...
我首先通过设置npm依赖项(例如node_modules文件夹)来构建此应用程序。 然后,我专注于构建模板并连接复杂的api。 然后,我专注于JavaScript,以确保可以解析json对象。 然后我在上面放一些样式并运行它,以确保...
node_modules/ - 包含开发所需的代码依赖项。 包括核心框架 Montage 和默认设置的针对移动设备优化的用户界面小部件 Digit。 由于 MontageJS 使用 CommonJS 模块系统,因此您可以利用 npm 生态系统获取其他模块。 ...
node_modules/ - 包含开发所需的代码依赖项。 包括核心框架 Montage 和默认设置的针对移动设备优化的用户界面小部件 Digit。 由于 MontageJS 使用 CommonJS 模块系统,因此您可以利用 npm 生态系统获取其他模块。 ...
node_modules/ - 包含开发所需的代码依赖项。 包括核心框架 Montage 和默认设置的针对移动设备优化的用户界面小部件 Digit。 由于 MontageJS 使用 CommonJS 模块系统,因此您可以利用 npm 生态系统获取其他模块。 ...
node_modules/ - 包含开发所需的代码依赖项。 包括核心框架 Montage 和默认设置的针对移动设备优化的用户界面小部件 Digit。 由于 MontageJS 使用 CommonJS 模块系统,因此您可以利用 npm 生态系统获取其他模块。 ...