Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use in browser #21

Closed
siddo420 opened this issue Jul 22, 2015 · 9 comments
Closed

how to use in browser #21

siddo420 opened this issue Jul 22, 2015 · 9 comments

Comments

@siddo420
Copy link

I am writing test cases using Jasmine and need to use ajv through my browser.

I have already attempted to use it with browserify but it doesn't seem to work. Please provide a quick example as to how to use it within browser after bundling the package using browserify.

Here is what I have done so far:

browserify node_modules/ajv/lib/ajv.js -o node_modules/ajv/lib/bundle.js

and added that into my jasmine Spec file using:

  <script src="../../node_modules/ajv/lib/bundle.js"></script>

Then??

@epoberezkin
Copy link
Member

We use it with browserify with require('ajv') in dependent module, not as a separate bundle, but it should work the way you do it too. Are you using the latest version? it only works from 0.5.2.

@siddo420
Copy link
Author

Yes, using the latest versions of browserify and ajv.

@epoberezkin
Copy link
Member

ah, yes, it doesn't create any global at the moment. You can:

  1. require it directly from your code as we do
  2. create file bundle.js like this:
window.Ajv = require('ajv');

and then browserify and use this bundle

@siddo420
Copy link
Author

It looks like Browserify does not make require function available with the command I was using. Here is what I used instead:

browserify -r ./node_modules/ajv/lib/ajv.js:ajv -o ./node_modules/ajv/lib/bundle.js

Then, I was able to use it normally (after including bundle.js)

    Ajv                  = require('ajv')
  , ajv                  = Ajv()

@epoberezkin
Copy link
Member

Cool, thanks. I will add to readme. Just to confirm, you are still loading bundle in a separate script tag, correct?

@siddo420
Copy link
Author

Yes, I still load bundle.js in a script tag in HTML file.

  <script src="node_modules/ajv/lib/bundle.js"></script>

For some reason, many of the browserify articles do not mention -r option.

@epoberezkin
Copy link
Member

Thanks, added "Using in browser" to readme.

@nsdelgadov
Copy link

nsdelgadov commented Jan 17, 2017

@epoberezkin Can you add a link to this thread in the readme please? This help me a lot :D
PD: sorry for my english.

@epoberezkin
Copy link
Member

@nsdelgadov it's changed since then - the supplied bundle already creates global (it is "universal"). So if you load bundle from npm package or from cdnjs you can just use global Ajv. Maybe just one line comment can be added to readme.

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

No branches or pull requests

3 participants