简体   繁体   English

如何为Node.js应用程序管理基于requireJS的软件包

[英]How to manage requireJS based packages for Node.js applications

Since coming across RequireJS I have started to adopt it wholeheartedly as it seems a great way of organising dependencies etc. 自从遇到RequireJS之后,我就开始全心全意地采用它,因为这似乎是组织依赖项等的好方法。

For my current project I have created a 'package' of requireJS-organised modules, which will provide the needed database API, to many node.js applications. 对于我当前的项目,我创建了一个由requireJS组织的模块的“包”,该模块将为许多node.js应用程序提供所需的数据库API。

But I have come across a stumbling block ... how can I allow third party applications to use my package, without needing to faff with requireJS? 但是我遇到了一个绊脚石……如何允许第三方应用程序使用我的程序包,而无需使用requireJS?

My directory structure for my applications and API is currently as follows: 我的应用程序和API的目录结构当前如下:

api_package/node_modules
api_package/controllers/*
api_package/views/*
api_package/helpers/*
api_package/models/*
api_package/main.js

application_1/node_modules
application_1/app.js

application_2/node_modules
application_2/app.js

I need my applications to be self-contained - so they can be easily deployed - so my current work around is to copy api_package/* into the 'node_modules' directory of application_1 & 2 and setting the their requirejs config to the following: 我需要我的应用程序是独立的-以便可以轻松部署-所以我目前的解决方法是将api_package / *复制到application_1&2的'node_modules'目录中,并将它们的requirejs配置设置为以下内容:

(function() {
  var requirejs;

  requirejs = require('requirejs').config({
    baseUrl: __dirname,
    nodeRequire: require,
    packages: [
      {
        name: 'api_package',
        location: './node_modules/api_package'
      }
    ]
  });

This feels a little dirty and wrong! 这感觉有点肮脏和错误!

Is there a better way? 有没有更好的办法? Am I missing some packaging feature for requireJS? 我是否缺少requireJS的某些包装功能?

Is it even possible to hide the implementation details of my api_package (the detail being that I am using requireJS) and allow applications to use it as they would any other module: 甚至可以隐藏api_package的实现细节(细节是我正在使用requireJS)并允许应用程序像使用其他模块一样使用它:

require('api_package')

You can use the amdefine package, which allows you to code to the AMD API and have the module work in node programs without requiring those other programs to use AMD. 您可以使用amdefine软件包,该软件包允许您对AMD API进行编码,并使该模块在节点程序中运行,而无需其他程序使用AMD。

In addition to the documentation on the amdefine github page , this is also documented on the RequireJS website . 除了amdefine github页面 上的文档外,RequireJS网站上也有此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM