一个项目,某个模块的副本可能超过10个,太浪费空间了:(
嵌套? 是你自己不会写而已。
编程语言if else也能嵌套,但是会写的就不会多少嵌套。
node_modules 在规范上应该只有1层才对
你在模块里又放入node_modules文件夹,本身就是错误的。全局只应该有1个node_modules文件夹,所有的外部模块通过package管理。
你的系统应该类似这样放置:
-node_modules- a,b,c,... // 任何第3方模块
-lib // 你自己的代码
-src // 你自己的c代码
-... // 其他文件夹
还有,如果你只是发布单个模块,文件里不应该带node_modules,而应该通过package.json指明你的依赖。 这样别人的node_modules文件里会自动加入依赖。
@tulayang ⊙_⊙b汗,A/B B/C C/A A依赖B,B依赖C C依赖A 目录结构会是A/node_modules/B/node_modules/C/node_modules/A 。。。
@kingapple 跟你说了,这是错误的。 只要你的项目全局有一个node_modules,其他模块都会自动到这个文件夹中去挂载依赖。
而且这种方式,跟C语言的加载是很相似的。
@tulayang 看一下这个
hexo\node_modules\gulp-jshint\node_modules\gulp-util\node_modules\chalk\node_modules\strip-ansi\node_modules
亮瞎狗眼了:(
hexo -node_modules- ------------------------------gulp-jshint ------------------------------gulp-util ------------------------------chalk ------------------------------strip-ansi ------------------------------...
@kingapple 如果是你 你会放过这个优化的机会么?https://github.com/joyent/node/blob/master/lib/module.js#L280
@yorkie if (cachedModule) { return cachedModule.exports; }
if (NativeModule.exists(filename)) { // REPL is a special case, because it needs the real require. if (filename == ‘repl’) { var replModule = new Module(‘repl’); replModule._compile(NativeModule.getSource(‘repl’), ‘repl.js’); NativeModule._cache.repl = replModule; return replModule.exports; }
debug('load native module ' + request);
return NativeModule.require(filename);
}
棒棒
@kingapple if (cachedModule) { return cachedModule.exports; }
if (NativeModule.exists(filename)) { // REPL is a special case, because it needs the real require. if (filename == ‘repl’) { var replModule = new Module(‘repl’); replModule._compile(NativeModule.getSource(‘repl’), ‘repl.js’); NativeModule._cache.repl = replModule; return replModule.exports; }
debug('load native module ' + request);
return NativeModule.require(filename);
}
这个绝对是node.js的优势,这样做非常好,各个项目依赖的包版本号可能不相同,很好的独立各个项目,不像Python为了这个功能还需要搞一个 VirtualEnv 来管理各个项目的依赖包版本,node.js原生支持还不满足?
只能说 npm 和 node不是一起做的,npm的install -g 装到AppData\Roming\npm下面
node的require有全局路径 C:\user\Administrator.node_modules\ 但是npm 硬是自己搞一个
不爽npm可以自己搞一个出来吗,再搭个registry,就行了
@tulayang @tulayang 其实楼主所说的情况很常见,并不奇怪。比如模块A依赖模块B,而模块B又依赖模块C,模块C又依赖模块D。然后在npm install的时候就会出现那样的文件目录,因为每个npm模块都应该有个package.json。 而安装的时候默认就是安装在其所在目录下。
但是楼主所担心的问题,nodejs在设计时候也已经考虑到了。
模块的加载机制,模块首次使用的时候,会缓存此模块编译好的文件。当require的时候首先是从缓存中获取,而缓存的模块其实是编译好的,这也大大减少了加载和编译的时间。其次才会去加载核心模块,自定义模块和文件路径模块。具体的加载机制你可以在google下
好消息...lz 的愿望在未来的npm 3.0 将会实现...
http://tw.iojs.org/2015/02/24/npm-weekly-6/
ps: 看来npm现在有钱,有时间,有人开发复杂功能了...
这不是node的问题,你要是不嫌麻烦,把嵌套的node_modules里边的模块全部拷贝到根目录的node_modules里node运行也是没问题的。 楼主说的问题确实存在,windows用户应该经常碰到拷贝或者删除node_modules目录时系统报路径太长的错误,但那是npm的问题。
这么做的好处显而易见,a 依赖 b,c 依赖另一个版本的 b,两个版本的 b 差了一个大版本,不兼容。python 就很蛋疼,所有大环境只能用一套版本的包管理都很蛋疼。我是一个被 npm 宠坏的孩子,就是因为 node_modules。