4

When I try to use js-cookie, my javascript file can't seem to track down the Cookies variable from js.cookie-js.js. In my index.html file, I reference the script using this line of code:

<script src="/node_modules/js-cookie/src/js.cookie.js"></script>

In my index.js file, the first time I reference js-cookie, in order to check if any cookies have been established, I call this line of code:

if (!(Cookies.get('user_id'))) {

From there, I always get the following error:

ReferenceError: Cookies is not defined
    at Namespace.<anonymous> (/Users/sourabh/sourabhmarathe.github.io/index.js:16:2)
    at emitOne (events.js:116:13)
    at Namespace.emit (events.js:211:7)
    at Namespace.emit (/Users/sourabh/sourabhmarathe.github.io/node_modules/socket.io/lib/namespace.js:213:10)
    at /Users/sourabh/sourabhmarathe.github.io/node_modules/socket.io/lib/namespace.js:181:14
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickCallback (internal/process/next_tick.js:181:9)
Allan Lewis
  • 308
  • 3
  • 13
Marty
  • 133
  • 1
  • 2
  • 7

2 Answers2

2

Probably you're not loading the right library for that, here is the cdn to load before your rules:

<script type="application/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
Luciano Santos
  • 153
  • 2
  • 12
0

This means that Cookies is being treated as a variable. However at the time of running the code, that variable has the value undefined. You have to see where that variable is being set. Make sure that the execution order of the code always defines the variable first before using it.

Without seeing the rest of the code, I can't give you an exact remedy.

Tamb
  • 748
  • 11
  • 20
  • 1
    In my code, I don't set anything equal to Cookies. Do you think that I need a line of code like `var Cookies = require('js-cookie')`? I don't see anything of the like from js-cookie's github page. – Marty Oct 18 '18 at 18:22
  • For me, I have made it worked like so : `import Cookies from 'js-cookie';` – Patrice Poliquin Mar 23 '21 at 12:49