Gracefully handle files that cannot be required
See original GitHub issueI 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:
- Created 9 years ago
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have the same problem browserifying
ws
. It contains the following file:I think it should be possible for browserify to detect
require
s inside atry
/catch
, and automaticallyexclude()
it if it can’t be found (notignore()
, 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.
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 atry
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 thecatch (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 optionignoreMissing
, perhaps the following could be added to therequire
function?OR
With this setup:
item.js
is not found the bundle breaks (the way Browserify already works).local-settings.js
is not found the bundle would just returnundefined
(ornull
) but not break allowing you to optionally have the filelocal-settings.js
.Except for the terrible name
dontBreakIfNotFound
😏 what do you think of either setup @jmm and @jscissr or does this already exists?