Repository: browserify/browserify (JavaScript)

Issue:
Browserify tolerates require() of invalid JSON
This is `browserify@13.0.1` and Node.js v6.2.2.

`file.json`:

```
{
  key: "value"
}
```

`index.js`:

```
require("./file.json");
```

Command line:

```
browserify index.js
```

Expected output: some kind of error because of the invalid JSON?

Actual output:

```
(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){
module.exports={
  key: "value"
}

},{}],2:[function(require,module,exports){
require("./file.json");
},{"./file.json":1}]},{},[2]);
```



Discussion:
Hello @s100, thanks for the report! I agree, that is uncool. We could solve this by trying to parse as JSON at bundling time in the `json` phase of the pipeline, or defer it to runtime, or if necessary provide an option to toggle. Would you be interested in working on a failing test and / or PR for this?

Unfortunately I don't know the first thing about Browserify's internals, I am just a humble user.

Ok, no problem, just wanted to give you the first crack at it :smiley:




Patch:
diff --git a/index.js b/index.js
index 858a0cbbf..9494152e1 100644
--- a/index.js
+++ b/index.js
@@ -662,7 +662,16 @@ Browserify.prototype._recorder = function (opts) {
 Browserify.prototype._json = function () {
     return through.obj(function (row, enc, next) {
         if (/\.json$/.test(row.file)) {
-            row.source = 'module.exports=' + sanitize(row.source);
+            var sanitizedString = sanitize(row.source);
+            try {
+                // check json validity
+                JSON.parse(sanitizedString);
+                row.source = 'module.exports=' + sanitizedString;
+            } catch (err) {
+                err.message = 'While parsing ' + (row.file || row.id) + ': ' + err.message
+                this.emit('error', err);
+                return;
+            }
         }
         this.push(row);
         next();


Test patch:
diff --git a/test/json.js b/test/json.js
index e0f2e27ea..0ec722028 100644
--- a/test/json.js
+++ b/test/json.js
@@ -46,3 +46,12 @@ test('evil json', function (t) {
         vm.runInNewContext(src, c);
     });
 });
+
+test('invalid json', function (t) {
+    var b = browserify();
+    t.plan(1);
+    b.add(__dirname + '/json/invalid.js');
+    b.bundle(function (err, src) {
+        t.ok(err);
+    });
+});
diff --git a/test/json/invalid.js b/test/json/invalid.js
new file mode 100644
index 000000000..36ef338f4
--- /dev/null
+++ b/test/json/invalid.js
@@ -0,0 +1,2 @@
+ex(require('./invalid.json'));
+ex(require('./invalid'));
diff --git a/test/json/invalid.json b/test/json/invalid.json
new file mode 100644
index 000000000..fb73e57aa
--- /dev/null
+++ b/test/json/invalid.json
@@ -0,0 +1,3 @@
+{
+  key: "value"
+}
\ No newline at end of file