简体   繁体   English

让RingoJS在node_modules目录中寻找所需的模块

[英]Getting RingoJS to look for required modules in the node_modules directory

I'm trying to migrate a service from nodejs to ringojs. 我正在尝试将服务从nodejs迁移到ringojs。 I've run into a problem with using require() . 我在使用require()遇到了问题。 For example consider this: 例如,考虑以下情况:

var restify = require('restify');

RingoJS cannot find the restify module because it doesn't know to look in the node_modules directory. RingoJS找不到restify模块,因为它不知道在node_modules目录中查找。 I can add node_modules to the path that RingoJS uses (and I did), but that doesn't help when restify calls require() , because the modules are nested in the directory tree. 我可以将node_modules添加到RingoJS使用的路径(我也这样做),但这在重新验证调用require()时无济于事,因为模块嵌套在目录树中。

Is there a way to get RingoJS to look for required modules in the node_modules directory? 有没有办法让RingoJS在node_modules目录中寻找所需的模块?

You can add additional directories to Ringo's module search path with the -m option. 您可以使用-m选项将其他目录添加到Ringo的模块搜索路径。 Example: Install underscore via npm with npm install underscore and start ringo with ringo -m ./node_modules yourscript.js . 例如:安装underscore通过NPM npm install underscore ,并开始与林檎ringo -m ./node_modules yourscript.js Undersocre will be available and can be required as expected: Undersocre将可用,并且可能会按要求提供:

    const _ = require("underscore");
    // logs --> "3,6,9"
    console.log(_.map([1, 2, 3], function(num) {
       return num * 3;
    }));

Your specific problem seems to be that restify is only compatible with Node.js and not with other CommonJS-like platforms. 您的特定问题似乎是restify 与Node.js兼容,而不与其他类似CommonJS的平台兼容。 It might have some sub-modules compatible with Ringo, but I haven't found any so far. 它可能有一些与Ringo兼容的子模块,但到目前为止我还没有找到。 Ringo's package.json is not 1:1 compatible with Node's / npm's package.json . Ringo的package.json与Node的/ npm的package.json不1:1兼容。 If restify uses some Node-specific stuff in the package descriptor, Ringo cannot load these resources. 如果restify在包描述符中使用了一些特定于Node的东西,则Ringo无法加载这些资源。

Ringo is still very close to ideas behind CommonJS with its various standardized modules, whereas Node has left this path a long time ago. Ringo凭借其各种标准化模块仍非常接近CommonJS背后的想法,而Node早已离开了这条路。 You find CommonJS modules (which use require() to load a module) in Node, but not the different other APIs CommonJS tried to establish for server-side JavaScript. 您可以在Node中找到CommonJS模块(使用require()加载模块),但不是CommonJS尝试为服务器端JavaScript建立的其他API。

Also, Ringo is not built around callbacks / an event-driven non-blocking I/O. 另外,Ringo并不是围绕回调/事件驱动的非阻塞I / O构建的。 You can use non-blocking I/O on Ringo, but you also can stick with blocking I/O. 您可以在Ringo上使用非阻塞I / O,但也可以坚持阻塞I / O。 This is different with Node.js, where you have to develop in a non-blocking way and everything is optimized to run smooth with the event-driven model. 这与Node.js不同,在Node.js中,您必须以非阻塞方式进行开发,并且所有内容都进行了优化,以使事件驱动模型能够平稳运行。

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

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