Troubleshooting module not being found with rekuire, requirish, or rfr in resolving relative require problem in nodejs

Looking to steer clear of the complicated relative path problem detailed here by opting for one of the suggested solutions. I've found three similar libraries that could help:

I've experimented with all three and encountered "module not found" errors, leading me to believe I may be making a fundamental mistake. As someone relatively inexperienced with npm/node, I am solely using node in the browser and utilizing browserify to bundle my application into a single JS file.

Below is an extremely straightforward hello world example:

Structure:

lib/Bob.js
app.js

Bob.js

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

module.exports = Bob;

app.js

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

console.log(Bob());

Bundling into a single JS:

browserify app.js -o bundle.js

The console in Chrome successfully displays "I am Bob".


When attempting any of the libraries, such as requirish:

REQUIRISH:

npm install requirish

Changes to app.js

'use strict';

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

console.log(Bob());

Changes to bundling

browserify -t requirish app.js > bundle.js

This results in 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:

'use strict';

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

console.log(Bob());

Building process

browserify app.js -o bundle.js -d

The console in Chrome shows the following error message:

Uncaught Error: Cannot find module 'lib/Bob'

Answer №1

When using Browserify, modules can be found by parsing the string "require". To work on both client and server sides, consider using rfr for the server side and browserify-rfr as a transform for Browserify.

In my experience, "rfr" is preferable because it does not override the original require function.

------- Important! Additional Information, The current version of browserify-rfr passes my local file path to bundle.js. This could lead to issues, so I opted for requirish. Be aware that requirish alters the behavior of the original require method by adding a new path to module.paths, so make sure to inform your colleagues!

Thank you!

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The issue persists as AJAX data and text box data are not being saved simultaneously

I need assistance with an Ajax setup. I am trying to pass the screenwidth information along with a user input value from a text box to a PHP page. However, I am encountering issues as the only value being passed is from the textbox and the other one is sho ...

HTML/JavaScript: Embrace the Power of Dynamic Page

I have a unique element in my HTML code: <image src="http://..." style='...'> Using Python-Flask, I pass on a dynamic source address and save it as window.dynamicEmbedding. Now, during page load, I want to change the image's ...

Switch up the CSS variable within an embedded iframe

I'm in a predicament with a specific issue. I am currently utilizing Angular to incorporate an Iframe. Let's imagine the angular app as A and the Iframe as B. B is being loaded within A. Within B, I have utilized CSS variables to define colors. I ...

What is the role of the .connect() method in Web Audio nodes?

Following the instructions found here, which is essentially a copy and paste from this I think I've managed to grasp most of it, except for all the node.connect()'s As far as I can tell, this code sequence is necessary to supply the audio anal ...

Submit a set of form variables to PHP using AJAX

I am attempting to transmit a set of form parameters to a PHP script for processing. In the past, I have achieved this using $.post, but now my goal is to accomplish it exclusively with $.ajax. Below is the jQuery click event designed to send all variabl ...

JavaScript variable not refreshing its value after submitting an HTML form

In my HTML form, I have 8 textboxes enclosed in div tags with IDs ranging from fa1 to fa8. By default, two of the textboxes are visible while the remaining six are hidden. To toggle the visibility of these divs, I've implemented two buttons - addfa an ...

What could be causing my Ajax JSON data to not return accurately despite appearing correctly in the console logs?

My function is designed to retrieve a number (1,2,3, etc.) based on latitude and longitude coordinates: function getNumber(lat,lng) { var params="lat="+lat+"&long="+lng; $.ajax({ type: "POST", url: "https://www.page.com/cod ...

Is there a way to shut down a browser tab without being asked, "Are you sure you want to close this window"?

Is there a way to gracefully close a browser window without being interrupted by the annoying Do you want to close this window prompt? I've noticed that whenever I attempt to use the window.close(); function, this prompt always pops up. ...

What is the best way to send the index variable to an HTML element in Angular?

Is there a way to pass the index variable to construct HTML in the append() function? .directive('grid', ['$compile', function(compile) { return { restrict: "E", scope: { elements: '=' ...

Creating a custom jQuery plugin for exporting data

I am crossing my fingers that this question doesn't get marked as 'already answered' because I have thoroughly searched previous questions and unfortunately, my specific case is not listed anywhere. I have successfully created a jQuery func ...

What is the best way to reset an HTML file input using JavaScript?

Looking for a way to reset the file input in my form. I've tried setting the sources to the same method, but it doesn't delete the selected file path. Important: Trying to find a solution without having to reload the page, reset the form, or us ...

When using Jquery, hovering over an element will cause the full title to be

I have created a toggle div that displays an ellipsis (...) when the long title is longer than 30 characters. Now, I want the full text of the long title to appear when hovering over the div. Check out this JS Fiddle for reference. JS $('#popu ...

When placed in a conditional, the type of the body variable changes from a string to undefined

Unexpectedly, the final if block fails to retain the value of the body variable and transforms it into undefined. While the console log statement just before the block correctly displays the type of the variable as a "string", during the condition check an ...

Is there a better method to accomplish this task in a more effective manner?

Is there a more efficient way to achieve this code with fewer lines of code? I'm looking for a solution that avoids repetition and makes it easier to manage, especially since I plan on creating multiple instances of this. Performance is a key consider ...

Locate an element within an array of strings to refine the contents of a flatlist

Currently, I have a FlatList that I am attempting to filter using multiple inputs from dropdown selectors. Here is the code snippet for my FlatList component: <FlatList style={styles.list} data={data.filter(filteredUsers)} ...

Retrieving information from a JSON file and dynamically displaying it on an HTML page using JavaScript

I've been working on pulling data from a JSON file to display on my website. Following this helpful guide at: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON, but unfortunately, I'm facing an issue where nothing is showing ...

Is it more suitable for a library used by getStaticProps to be classified as a normal dependency or a dev

When working with NextJS's getStaticProps, I am implementing a library that is only utilized during build time. Should this library be categorized as a regular or development dependency in my package.json? ...

How do popular social media platforms such as Facebook and Twitter utilize real-time APIs to display live updates in news feeds

I'm curious about the technology being used for real-time updates, and I doubt they rely on AJax methods like setInterval() to make server requests every second. This approach could be inefficient with a large number of concurrent users. Do you think ...

If the user fails to respond, Alexa will show an error message indicating that the skill response was marked as a failure

After completing the code for my Alexa skill, I encountered a problem. My fact-based skill template is set to wait for responses after the output, but if Alexa doesn't hear a response within 8 seconds, it generates an error (Skill response was marked ...

jquery activating the toggle function to switch between child elements

I'm facing a challenge where I can't use .children() in this scenario, as it won't work since the elements aren't technically children. Here's the snippet of HTML that I'm working with: <p class="l1">A</p> ...