用于复杂应用程序结构的NodeJS本地模块 [英] NodeJS local modules for complex application structures

查看:100
本文介绍了用于复杂应用程序结构的NodeJS本地模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前是使用JavaScript构建Windows 8应用程序团队的一员。
我们使用npm和browserify来管理依赖关系并将我们的模块转换为AMD浏览器友好格式。

I'm currently part of team building a Windows 8 application using JavaScript. We are using npm and browserify to manage dependencies and convert our modules to AMD browser friendly format.

我们遇到的一个问题是疯狂的需求路径。这是因为我们的应用程序组件中有一个顶级文件夹。该文件夹包含一堆嵌套的ui组件/模块。这些模块有时需要lib utils和helper,它们位于lib目录中。

One issue we are running into is crazy require paths. This is because we have a top level folder inside our application "components". This folder contains a bunch of nested ui components/modules. These modules sometimes require lib utils and helpers, which reside in the lib directory.

例如,生成my / app / components / product / grid /的模块item可能需要一个位于my / app / lib / helpers / view的辅助模块。

So for example, a module living in "my/app/components/product/grid/item" might require a helper module which is located "my/app/lib/helpers/view".

需求路径有点疯狂且非常难看:
require(../../../../ lib / helpers / view);

The require path is a bit crazy and very ugly: require("../../../../lib/helpers/view");

我们正在尽力建立应用程序模块化时尚。现在我认为解决这个问题的正确方法是让我们的组件模块依赖于这些util辅助模块。我可以把lib帮助器放到他们自己的外部私有git仓库中,但是在给其他团队访问方面一直很痛苦(加上git private repos很慢)。此外,由于这些模块仅用于应用程序,因此进行更改,推送更改,然后返回应用程序和npm更新是浪费时间。这对某些人来说很好,但是如果我们真的打破了它,它可能会变得很快。

We are doing a best to build in application in modular fashion. Now I would think the proper way to approach this is to have our components modules depend on these util helper modules. I could put the lib helpers into their own external private git repo, but that has been pain in terms of giving other teams access (plus git private repos are slow). Plus since those modules are only used in the application, it's a waste of time to make the change, push the changes, then go back to the application and npm update. This is fine for some, but if we really break this down, it could get old real fast.

我可以做npm installmy / app / lib / helpers /查看组件内部的package.json?但是npm install不会自动为我们这样做。

I could do npm install "my/app/lib/helpers/view" inside the components package.json ? But npm install won't automatically do this for us.

我知道其他一些方法(NODE_PATH,也许使用npm安装钩子或者npm预安装脚本),但想知道是否有其他人有类似的问题和良好的解决方案。

I know of a few other ways around this (NODE_PATH, maybe use a npm install hook or maybe npm preinstall script), but wanted to know if anyone else had a similar problem and good solution.

推荐答案

你可以把你的my / app / components / product / grid / item文件到 node_modules / grid / item.js 然后当你 require('grid / item')在您的应用程序代码中,您将获得所需的文件,并且需要更多的terser require路径语法。只需检查 node_modules / grid / item.js 以及其他任何文件到git中。 node_modules / 目录甚至不需要位于顶层,因为node和browserify使用的require算法将搜索 node_modules / 从当前路径一直到 / 的目录,直到找到匹配的模块。

You can put your "my/app/components/product/grid/item" files into node_modules/grid/item.js and then when you require('grid/item') in your application code you will get the file you want with a much terser require path syntax. Just check node_modules/grid/item.js and whichever other files into git. The node_modules/ directory need not even be at the top-level since the require algorithm used by node and browserify will search for node_modules/ directories from the current path all the way down to / until it finds a matching module.

Just确保将grid添加到 <你的package.json中有code>bundledDependencies 数组,所以你不会在它上面意外安装。

Just make sure to add "grid" to the "bundledDependencies" array in your package.json so you don't accidentally install something over it.

您可以阅读有关将节点模块检入git 的更多信息。

You can read more about checking node modules into git.

阅读有关避免的浏览器手册部分。 ./../../../../../ 获取更多信息。

NODE_PATH总是一个坏主意,浏览器不会支持它。不要永远使用它。

NODE_PATH is always a bad idea and browserify doesn't support it. Don't ever use it ever.

这篇关于用于复杂应用程序结构的NodeJS本地模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆