Skip to content

Instantly share code, notes, and snippets.

@alexindigo
Created February 2, 2016 16:49
Show Gist options
  • Save alexindigo/91dd66c3267395a27f88 to your computer and use it in GitHub Desktop.
Save alexindigo/91dd66c3267395a27f88 to your computer and use it in GitHub Desktop.
Issue with bundling nodejs modules with and without `nodeIdCompat` flag
// Filepath: node-modules/my_module/index.js
var mutable = require('./mutable.js')
, defaults = require('./defaults.json')
;

This is how my file looks, and when I'm trying to bundle it up, it gives me error that it couldn't find my_module/mutable

Error: ENOENT, no such file or directory '/www/my_module/mutable.js'

If I set nodeIdCompat:true it will give me this error:

Error: ENOENT, no such file or directory '/www/my_module/defaults.json.js'
@jrburke
Copy link

jrburke commented Feb 2, 2016

The ID compatibility is really just treating ".js" as not part of the module ID, and not following the normal requirejs route of treating it as a plain URL that does not get baseUrl and ID-to-path translation.

It is not aware of .json files, and even if it was, it would not treat those as JSON files, as in use an XHR to load them and JSON.parse() to convert that to a module value. A 'json!' loader plugin should be used for that, and since a loader plugin is needed, the nodeIdCompat is not focused on .json file suffixes. I'll update the http://requirejs.org/docs/api.html#config-nodeIdCompat section to mention this.

@jrburke
Copy link

jrburke commented Feb 2, 2016

Just pushed an update to the doc section for the option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment