在node.js模块之间共享下划线mixins(Share underscore mixins across node.js modules)

编程入门 行业动态 更新时间:2024-10-22 21:19:24
在node.js模块之间共享下划线mixins(Share underscore mixins across node.js modules)

我想从一个模式子模块与其父模块共享一个下划线mixin。 这是我的设置:

. ├── index.js └── node_modules └── submodule ├── index.js ├── node_modules │   └── underscore │   ├── LICENSE │   ├── README.md │   ├── package.json │   ├── underscore-min.js │   └── underscore.js └── package.json

./index.js:

var submodule = require('submodule') , _ = require('underscore'); console.log('In main module : %s', _.capitalize('hello'));

./node_modules/submodule/index.js:

var _ = require('underscore'); _.mixin({ capitalize : function(string) { return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase(); } }); console.log('In submodule : %s', _.capitalize('hello'));

当我运行node index.js我得到以下输出:

In submodule : Hello /Users/lxe/devel/underscore-test/index.js:4 console.log('In main module : %s', _.capitalize('hello')); ^ TypeError: Object function (obj) { if (obj instanceof _) return obj; if (!(this instanceof _)) return new _(obj); this._wrapped = obj; } has no method 'capitalize'

如您所见,mixin在子模块中注册( In submodule : Hello )。 但是, _.capitalize在主模块中未定义。

如何让模块共享mixins?

I'd like to share an underscore mixin from a mode submodule with its parent module. Here's my setup:

. ├── index.js └── node_modules └── submodule ├── index.js ├── node_modules │   └── underscore │   ├── LICENSE │   ├── README.md │   ├── package.json │   ├── underscore-min.js │   └── underscore.js └── package.json

./index.js:

var submodule = require('submodule') , _ = require('underscore'); console.log('In main module : %s', _.capitalize('hello'));

./node_modules/submodule/index.js:

var _ = require('underscore'); _.mixin({ capitalize : function(string) { return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase(); } }); console.log('In submodule : %s', _.capitalize('hello'));

When i run node index.js I get the following output:

In submodule : Hello /Users/lxe/devel/underscore-test/index.js:4 console.log('In main module : %s', _.capitalize('hello')); ^ TypeError: Object function (obj) { if (obj instanceof _) return obj; if (!(this instanceof _)) return new _(obj); this._wrapped = obj; } has no method 'capitalize'

As you can see, the mixin was registered in submodule (In submodule : Hello). However, _.capitalize is undefined in the main module.

How can I make the modules share the mixins?

最满意答案

我想我想通了! 我需要改变一下我的树:

├── index.js └── node_modules ├── submodule │   ├── index.js │   └── package.json └── underscore ├── LICENSE ├── README.md ├── package.json ├── underscore-min.js └── underscore.js

现在只有根模块具有'下划线'模块。 我猜测在子模块中执行require('下划线')要么使用主模块中的require的缓存,要么向上遍历树以找到它。

I think I figured it out! I needed to change my tree a bit:

├── index.js └── node_modules ├── submodule │   ├── index.js │   └── package.json └── underscore ├── LICENSE ├── README.md ├── package.json ├── underscore-min.js └── underscore.js

Now only the root module has the 'underscore' module. I'm guessing doing require('underscore') in the submodule either uses a cache of the require from the main module, or traverses the tree upwards to find it.

更多推荐

本文发布于:2023-08-01 18:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1363749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下划线   模块   js   node   modules

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!