question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't import npm package using es6 module

See original GitHub issue

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?

Issue Analytics

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

github_iconTop GitHub Comments

59reactions
ThaNariecommented, Jul 17, 2016

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 😃

12reactions
aymericbouzycommented, Apr 13, 2017

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

import Cookie from 'js-cookie'

Cookie.get("foo")
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot Import My NPM Package With ES6 Import/Export Syntax
I want it to work WITHOUT webpack. This is why this is partially solved. javascript · npm · ecmascript-6 · module · importerror....
Read more >
Cannot import modules installed with NPM - Sololearn
I'm using ES6 import keyword, I have tried this with a various of different packages, but it just doesn't work, for example, I...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export...
Read more >
Difference between node.js require and ES6 import and export
ES6 Import & Export: These statements are used to refer to an ES module. Other file types can't be imported with these statements....
Read more >
can not use import statement outside a module - YouTube
JS - JavaScript. syntaxerror - can not use import statement outside a module | es6 vs commonjs modules in nodejs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found