require icon indicating copy to clipboard operation
require copied to clipboard

Don't abort if core node module isn't found

Open harthur opened this issue 14 years ago • 7 comments

First of all, thanks for the lib. I think it's exactly what I'm looking for with building browser files.

My node package conditionally requires a core node module if the library isn't being used in the browser. The node module can't be found so require aborts with "Cannot find module 'net'". For this use case it would be nice if it could just warn instead.

harthur avatar May 16 '11 04:05 harthur

hmm, got it. you know, I'm not so sure a warning is the right thing to do in that case. hear me out.

in node the require statement always throws if a module can't be found. don't know about rhino, but I'd expect the same thing there. if you require a module it really should be there, or tell you loudly that what you required can't be loaded.

Is it possible for you to do a conditional require instead, where you load the net module iff you're in node?

if you want to convince me I'm wrong I'm around the corner from the mv office

marcuswestin avatar May 16 '11 08:05 marcuswestin

Maybe I'm understanding wrong, but the require is conditional as far as I'm concerned. If you're in the browser require('net') is never hit. It's more complicated than that, but the line in question is here: https://github.com/harthur/brain/blob/master/lib/bayesian/bayesian.js#L13.

Require.js though replaces every require statement, so it tries to find the net module (which is a line in the Redis lib).

harthur avatar May 16 '11 17:05 harthur

Ah, yeah you're totally right. Let me think about it

marcuswestin avatar May 16 '11 17:05 marcuswestin

You'll probably think I'm just stubborn, but I think the way to go is to create to different calling scripts, e.g. bayesian-browser and bayesian-node

bayesian-node.js

var bayesian = require('bayesian'), redis = require('redis') new bayesian.BayesianClassifier(new redis.RedisBackend())

marcuswestin avatar May 16 '11 17:05 marcuswestin

Hm, I don't think I'm going to do that. I might just string replace the require statement before building.

harthur avatar May 16 '11 18:05 harthur

How about a flag to not exit on a failed import?

marcuswestin avatar May 16 '11 22:05 marcuswestin

That could work. I think at this point though I don't even want to include the redis lib, so I'll probably just parse out the require.

harthur avatar May 17 '11 04:05 harthur