js-cookie: Can't import npm package using es6 module

Hi, sorry for a stupid question, but I just can’t seem to import the Cookies object in my project no matter what I try.

Here is what I tried:

import 'js-cookie'

and

import Cookie from 'js-cookie'
window.Cookie = Cookie

None produce any Cookie names object in the window, or local context. I can import any other packages just fine, but somehow not this one.

Any ideas why that might be?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

The es6 module spec doesn’t have anything similar to the module.exports = obj, there’s only default and named exports.

Fortunately, you can still fake it by doing:

import * as Cookies from "js-cookie";

In this case, all the variable and functions from the exported object will be ‘transferred’ over the the Cookies object.

Note: If you are using TypeScript, use the --allowSyntheticDefaultImports flag.

So no code changes needed, adding this usage to the docs however would be nice 😃

I just installed version 2.1.4 and the following works for me with webpack :

import Cookie from 'js-cookie'

Cookie.get("foo")

So what I end up doing was adding a file in my projects where it says

window.Cookies = require('js-cookie');

And importing it from the main file where I import stuff.

@ThaNarie

If you like I can edit my linked comment to add this info.

That can be useful for those coming from a search engine.

Oh, in that case it would be nice to have a big warning somewhere so that people don’t waste time banging their haid on the desk like I did 😃