简体   繁体   English

使用rekuire,requirish或rfr解决Node.js时找不到模块

[英]Module not found when using rekuire, requirish, or rfr to solve nodejs require relative issue

I'd like to avoid the complex relative path issue described here by using one of the recommended solutions. 我想通过使用推荐的解决方案之一来避免此处描述的复杂的相对路径问题。 I've come across three similar libraries: 我遇到了三个类似的库:

I've tried all three and all are failing with "module not found" or a similar error which makes me believe I'm doing something fundamentally wrong. 我已经尝试了所有这三个方法,但都因“找不到模块”或类似的错误而失败,这使我相信我做的事情根本上是错误的。 I'm relatively inexperienced with npm/node. 我对npm / node经验不足。 I'm only using node in the browser using browserify to bundle my app into a single JS file. 我只使用浏览器中的节点,使用browserify将我的应用程序捆绑到一个JS文件中。

Here's my extremely simple hello world example: 这是我非常简单的hello world示例:

Structure: 结构体:

lib/Bob.js
app.js

Bob.js Bob.js

function Bob() {
    return "I am bob";
}

module.exports = Bob;

app.js app.js

var Bob = require('./lib/Bob.js');

console.log(Bob());

Bundling into a single JS: 捆绑成一个JS:

browserify app.js -o bundle.js

Chrome's console successfully outputs "I am Bob". Chrome浏览器的控制台成功输出“我是鲍勃”。


Now if I try and of the libraries, let's say requirish: 现在,如果我尝试使用这些库,那么请问一下:

REQUIRISH: 要求:

npm install requirish

app.js changes app.js更改

'use strict';

require('requirish')._(module);
var Bob = require('lib/Bob');

console.log(Bob());

Bundling changes 捆绑变化

browserify -t requirish app.js > bundle.js

I get the following error: 我收到以下错误:

Error: Cannot find module '/lib/Bob' from '/Users/ngb/projects/MyApp/src/main/resources/public/js/hello'
at /Users/ngb/.nvm/v0.10.30/lib/node_modules/browserify/node_modules/resolve/lib/async.js:42:25

RFR: RFR:

'use strict';

var rfr = require('rfr');
var Bob = rfr('lib/Bob');

console.log(Bob());

Building 建造

browserify app.js -o bundle.js -d

Chrome's console outputs the following error: Chrome的控制台输出以下错误:

Uncaught Error: Cannot find module 'lib/Bob'

The Browserify can find module by parse string "require". Browserify可以通过解析字符串“ require”来找到模块​​。 If you want to use both side client and server, use rfr for server side and browserify-rfr for browserify's transform. 如果要同时使用客户端和服务器, 在服务器端使用rfr在browserify的转换中使用browserify -rfr

In my opinion, the "rfr" is the best because this module does not override original require. 在我看来,“ rfr”是最好的,因为该模块不会覆盖原始的需求。

------- Notice! - - - - 注意! Additional Information, As today browserify-rfr version leaves my local file path to bundle.js. 附加信息,因为今天的browserify-rfr版本将我的本地文件路径留给bundle.js。 This may cause another problem, so I chose requirish . 这可能会引起另一个问题,所以我选择了requirish Since the requirish changes behavior of original require by pushing a new path to module.paths, you always notice that and alert your coworker! 由于原始需求的行为改变是通过将新路径推送到module.paths来实现的,因此您始终会注意到这一点并警告您的同事!

thanks! 谢谢!

https://www.npmjs.com/package/requirish https://www.npmjs.com/package/requirish

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

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