ef4 / ember-browserify

ember-cli addon for easily loading CommonJS packages from npm via browserify.
MIT License
172 stars 28 forks source link

_dirname seems to confuse ember-browserify #68

Closed djjonbrown closed 7 years ago

djjonbrown commented 8 years ago

The first line of my imported module uses dirname: `var Parser = require(dirname + '/lib/Parser.js');`

ember-browserify seems to interpret __dirname in a strange manner: Uncaught Error: Could not find module /../../node_modules/acre-api/lib/Parser.js imported from (require)

modifying the first line of my imported module to use ./ instead fixes the issue: var Parser = require('./lib/Parser.js');

is this an issue with ember-browserify?

asakusuma commented 8 years ago

__dirname is a node-specific API. It's not going to work in the browser. It's a bad idea to use __dirname to resolve a path with require.

stefanpenner commented 8 years ago

It's not going to work in the browser.

this isn't correct, browserify emulates its functionality.

given:

console.log(__dirname);

and

browserify input.js > output.js

and the result is:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (__dirname){
__dirname

}).call(this,"/")
},{}]},{},[1]);

I suspect the library in question is using __dirname wrong, or maybe the path (where it is being browserified at|cwd) is funky.

@djjonbrown can you provide a concrete example?