question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gracefully handle files that cannot be required

See original GitHub issue

I have a file (settings.js) that is compiled by browserify. I want it to require a settings_local.js file only if if it exists. From the documentation and source code, I can’t figure out if this is possible. This is what I currently have:

var settings_local = {};
try {
  settings_local = require('./settings_local.js');
} catch (e) {
}

This is the error I get:

events.js:82
      throw er; // Unhandled 'error' event
            ^
Error: Cannot find module './settings_local.js' from '/opt/galaxy.js-mobile-gamepad/src/js'
    at /opt/galaxy.js-mobile-gamepad/node_modules/browserify/node_modules/resolve/lib/async.js:42:25
    at load (/opt/galaxy.js-mobile-gamepad/node_modules/browserify/node_modules/resolve/lib/async.js:60:43)
    at /opt/galaxy.js-mobile-gamepad/node_modules/browserify/node_modules/resolve/lib/async.js:66:22
    at /opt/galaxy.js-mobile-gamepad/node_modules/browserify/node_modules/resolve/lib/async.js:21:47
    at Object.oncomplete (fs.js:97:15)

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jscissrcommented, Jan 13, 2016

I have the same problem browserifying ws. It contains the following file:

try {
  module.exports = require('utf-8-validate');
} catch (e) {
  module.exports = require('./Validation.fallback');
}

I think it should be possible for browserify to detect requires inside a try/catch, and automatically exclude() it if it can’t be found (not ignore(), because then no error is thrown and you don’t get the fallback).

Do you think it’s possible? I would try to make a PR.

0reactions
hawkerboy7commented, Apr 9, 2020

Hi, I have been running into this issue as well and I a different solution approach which I feel is relatively easy to make and does not break backwards compatibility. I may be quite wrong though 😃

I think making Browserify detect a require within a try catch block would be a bit hard to make whilst keep everything working the way you’d expect it to work and remain backwards compatible. Also the catch (e) block would now somehow contain a server side error, while the bundle file is only executed by the browser, so you’d need browserify to add some error object containing possible try catch errors (with stack) and send it to the client which may not be ideal for production-environments.

What it boils down to I believe is that we’d like to be able to make the browserify.bundle() function continue the bundling process instead of breaking when a file is required that does not exsist / is not found. However this is not always the case for every file. So instead of hardcoding the list of ‘silently-continue-if-not-found’ files server-side using the option ignoreMissing, perhaps the following could be added to the require function?

var settings = {a: 1, b: 2};
var item = require("./item");
var localSettings = require.dontBreakIfNotFound("./local-settings");
if (localSettings !== undefined) {
  settings = localSettings;
};

OR

var settings = {a: 1, b: 2};
var item = require("./item");
var localSettings = require("./local-settings", {ignoreMissing: true});
if (localSettings !== undefined) {
  settings = localSettings;
};

With this setup:

  • if the file item.js is not found the bundle breaks (the way Browserify already works).
  • if the file local-settings.js is not found the bundle would just return undefined (or null) but not break allowing you to optionally have the file local-settings.js.

Except for the terrible name dontBreakIfNotFound 😏 what do you think of either setup @jmm and @jscissr or does this already exists?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Graceful exit - Wikipedia
A graceful exit (or graceful handling) is a simple programming idiom wherein a program detects a serious error condition and "exits gracefully" in...
Read more >
chapter 16 CSE Flashcards - Quizlet
An AttributeError occurs if a function does not exist in an imported module. Fill in the missing code to handle AttributeErrors gracefully and...
Read more >
Handle errors thrown by require() module in node.js
I want to be able to handle this error gracefully, instead of letting it stop my entire process.
Read more >
Is there a way to gracefully handle 1 missing bib file when ...
Using BibLatex and LyX requires that the path names to the bib files to be absolute references. So on Computer A, I need...
Read more >
graceful-fs - npm
This module cannot intercept or handle EMFILE or ENFILE errors from sync methods. If you use sync methods which open file descriptors then ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found