23

When I compile markdown-symbols using Browserify 3.30.2 (browserify file.js -o bundle.js), I get something like that :

!function(e){if("object"==typeof exports...[function(_dereq_,module,exports){

},{}],2:[function(_dereq_,module,exports){
...
...
[on line 8000] 
    : function (str, start, len) {
        if (start < 0) start = str.length + start;
        return str.substr(start, len);
    }
;

}).call(this,_dereq_("C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js"))
},{"C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js":11}],14:[function(_dereq_,module,exports){
module.exports=_dereq_(3)
},{}],15:[function(_dereq_,module,exports){
module.exports=_dereq_(4)
},{"./support/isBuffer":14,"C:\\Users\\ME\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js":11,"inherits":10}],16:[function(_dereq_,module,exports){
var frep = _dereq_('frep');
var file = _dereq_('fs-utils');
var delims = _dereq_('delims');
var _ = _dereq_('lodash');
...

As you can see, there are absolute paths to my files here. Why ? How can I remove them ?

EDIT: here is my build.js file

 var browserify = require('browserify-middleware')
fs = require('fs');
var b = browserify('./index.js', {
    'opts.basedir': './'
});
b({
    // Mocks up express req and res
    headers: []
}, {
    getHeader: function () {},
    setHeader: function () {},
    send: function (a) {
        console.log('send', a);
    },
    end: function (a) {
        //console.log('end', a.constructor.name);
        //  fs.write('bundle.js', a, undefined, undefined, function (err) {
        console.log(a.toString());
        //});
        //  a.pipe(fs.createWriteStream('bundle.js'));
    },

});

And to run node build > bundle.js. same problem. If I replace basedir value by for instancee ihatebrowserify there is an error about something not resolved.

5
  • What version of browserify are you using? Commented Feb 26, 2014 at 19:32
  • 3
    This is a bug in the insert-module-globals module. There is a pull request to fix it: github.com/substack/insert-module-globals/pull/31
    – Ross Allen
    Commented Apr 25, 2014 at 18:31
  • @ssorallen thnal you very much!
    – Vinz243
    Commented Apr 25, 2014 at 20:04
  • @Vinz243, did you ever get this working (i.e., no local paths leaking into the browserified js)? It's over 5 months since you asked this question now, and I'm getting the exact same symptom, using the most recent versions of everything... :(
    – Bruno Reis
    Commented Aug 8, 2014 at 5:41
  • Unfortunately no. I did it otherwise
    – Vinz243
    Commented Aug 8, 2014 at 13:37

6 Answers 6

7

This came across to me as well today. It turns out there's a boolean option --full-path[0] now that solves the problem.

For example:

browserify -o bundle.js --full-path=false index.js

[0] https://github.com/substack/node-browserify/blob/master/bin/args.js

2
  • well this removes the absolute paths but now i get Cannot read property 'addEventListener' of undefined at the top of my bundle.js ... :(
    – Unispaw
    Commented Nov 3, 2014 at 10:57
  • @Unispaw sounds like another problem. Did it appear only if you add --full-path=false switch?
    – Song Gao
    Commented Nov 4, 2014 at 3:54
5

Almost 6 months later, and I've seen the same issue. Now I found a workaround that suited me, and others might benefit from it as well.

Google has given me this issue report: https://github.com/thlorenz/browserify-shim/issues/43 -- which reports this local system path disclosure on "browserify-shim", although it is a "browserify" issue from what I understood.

That issue suggests an workaround:

$ npm install -g intreq browser-pack browser-unpack
$ browserify example/main.js -t browserify-shim | browser-unpack | intreq | browser-pack

I've tested this and was satisfied with the results. However I wanted to go further, and integrate this into my Gruntfile.js. The solution I found, tested and that made me happy is to use unpathify:

Compress browserify require paths for better minification i.e. require('some/long/path') => require(1)

To use it, just install unpathify (npm install --save-dev unpathify) and add it to your build:

grunt.initConfig({
  browserify: {
    all: {
      files: {
        'build/all.js': ['some/file.js']
      }
    }
  },
  unpathify: {
    all: {
      src: ['build/all.js']
    }
  }
  // ...
}
2

My best guess is that somewhere, there are modules being included from absolute paths.

It doesn't happen to me when using browserify.


Actually it's more than a guess.

},{}],2:[function(_dereq_,module,exports){
...
...
}).call(this,_dereq_("C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js"))
},{"C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js":11}],14:[function(_dereq_,module,exports){

You have _dereq_("C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\\browserify\\node_modules\\insert-module-globals\\node_modules\\process\\browser.js"); therefore, it must include that module.

If you set some Node global (no idea how on Windows) to look in C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules, you might not have to include the full path, but i can't be sure.

4
  • Thanks! I will search how to do it
    – Vinz243
    Commented Feb 26, 2014 at 19:32
  • You might also want to include the process package directly in your project. That should avoid the need for accessing the full path.
    – Scimonster
    Commented Feb 26, 2014 at 19:34
  • can it be related to sourcemaps ? github.com/substack/node-browserify/issues/663
    – Vinz243
    Commented Feb 27, 2014 at 18:20
  • please, just to help me, can you tell me how to do it (changing node global) on other OS? (I do have Windows, sure, but just for help)
    – Vinz243
    Commented Feb 27, 2014 at 18:22
2
+50

Browserify resolves absolute path as default. If you do not want to see absolute paths, you can set options.basedir as you want. For example;

var browserify = require('browserify-middleware');
var b = browserify({'opts.basedir': './'});
7
  • What should I put in your_path? C:\\Users\\Me\\AppData\\Roaming\\npm\\node_modules\`? '\. ? markdown-symbols? I put browserify in local modules.
    – Vinz243
    Commented Mar 5, 2014 at 9:51
  • Ah see, you have already stated base dir. However, I have answered your question before your edit. Commented Mar 5, 2014 at 12:21
  • @HüseyinBabel I don't really understand what you mean. Where did I stated base dir. In build.js on line 4 ('opts.basedir': './') ?Wasn't it what you mean ? In ./index.js? It i remove it, it doesn't work (exception). If I remove
    – Vinz243
    Commented Mar 5, 2014 at 12:35
  • In build.js, you have stated opts.basedir already. I said that, When I answer your question, There is no Edit section on your question Commented Mar 5, 2014 at 12:38
  • I don't understand. I put var browserify = require('browserify-middleware'); var b = browserify({'opts.basedir': './'});. I should remove opts.basedir?
    – Vinz243
    Commented Mar 5, 2014 at 12:43
2

Not entirely sure if this is an answer to the original question, but I was having a similar problem. The bundled js file and the external sourcemap (using exorcist) being generated was containing absolute paths to folders on my system. After a headache figuring out why, it seems the culprit was babelify (previously 6to5ify). https://github.com/substack/node-browserify/issues/663 & https://github.com/babel/babelify/issues/19

The solution is simple though:

browserify --debug -t [ babelify --sourceMapRelative . ]

or

browserify({ debug: true }) .transform(babelify.configure({sourceMapRelative: '.' }))

1
  • Thanks but i didn't used babelify
    – Vinz243
    Commented Mar 4, 2015 at 11:27
0

There is a new Browserify plugin available from the author of Browserify. It's called Bundle-Collapser. It'll replace folder paths with numerical indices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.