Skip to content

Instantly share code, notes, and snippets.

@mchelen
Forked from mchelen/.gitignore
Last active August 29, 2015 14:24
Show Gist options
  • Save mchelen/de7ec9b46ae4025d02a3 to your computer and use it in GitHub Desktop.
Save mchelen/de7ec9b46ae4025d02a3 to your computer and use it in GitHub Desktop.
jsonpath browserify
node_modules
bundle.js

Example trying to use jsonpath with browserify

https://www.npmjs.com/package/jsonpath

Install http-server and browserify globally:

npm install http-server browserify -g

Usage:

npm install
browserify main.js > bundle.js
http-server

Then open the localhost URL in a browser and look at the JS console

Error: Uncaught TypeError: require.resolve is not a function

Issue: jsonpath needs special Browserify options

https://github.com/dchester/jsonpath/blob/855ddc0a1a8138e8361492eb620fca6fe21d6300/Gruntfile.js#L16-L26

https://github.com/dchester/jsonpath/blob/855ddc0a1a8138e8361492eb620fca6fe21d6300/Gruntfile.js#L39-L46

<html>
<script src="bundle.js"></script>
</html>
var cities = [
{ name: "London", "population": 8615246 },
{ name: "Berlin", "population": 3517424 },
{ name: "Madrid", "population": 3165235 },
{ name: "Rome", "population": 2870528 }
];
var jp = require('jsonpath');
var names = jp.query(cities, '$..name');
console.log(names);
// [ "London", "Berlin", "Madrid", "Rome" ]
{
"name": "9249adace78338b670a4",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gist.github.com/9249adace78338b670a4.git"
},
"author": "Mike Chelen <michael.chelen@gmail.com> (https://github.com/mchelen/)",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/9249adace78338b670a4"
},
"homepage": "https://gist.github.com/9249adace78338b670a4",
"dependencies": {
"jsonpath": "^0.1.8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment