Cookies icon indicating copy to clipboard operation
Cookies copied to clipboard

Doesn't work in browser environment

Open wzup opened this issue 8 years ago • 1 comments

It doesn't work in browser environment. Or at least explain how to deploy it in a browser env. In your docs you write: cookies-js - google chrome 2017-01-20 11 03 59

So it is pretty natural to assume that in a browser it should be the same, since you do not provide any other info about how to run it in a browser:

const Cookies = require('cookies-js')(window);
window.Cookies = Cookies;

But the result is

Cookies === undefined.

How does it work in a browser?

wzup avatar Jan 20 '17 03:01 wzup

Hey @wzup - you've highlighted another confusing aspect of the documentation that makes me feel rather silly, seeing it now!

Right now the export for Cookies.js is different depending on if you're in a browser environment that has a global window object, or not. This difference is something that has bothered me for a while and I'd like to change eventually. In a non-browser environment, it's necessary to pass in some object to act as the window object, since it doesn't exist. However, this requirement is unnecessary when in the browser.

As a result, if you're in a browser, and you're not using any script loader (you're not using AMD or CommonJS), then Cookies.js will create a global Cookies object ready to go (you can immediately start calling things like Cookies.get and Cookies.set).

If you are using a script loader, then simply requiring the library is enough to start using it. For example, for CommonJS, simply do:

var Cookies = require('cookies-js');

Apologies for the confusion! This is something I'd like to make more consistent in the future. In the mean time, I should absolutely update the readme with more helpful usage instructions.

Thanks for checking out the library!

ScottHamper avatar Jan 20 '17 03:01 ScottHamper