This should be used in the README instead of the outdated dist approach:
import Cookies from 'js-cookie';
Could you point me to exactly what you’re referring to? I can’t follow. A pull request would also be highly appreciated.. thanks!
I am using this library inside a single page application and the current import example is just misleading from my point of view. There is no dist folder published in NPM also.
https://github.com/js-cookie/js-cookie#es-module
import Cookies from './node_modules/js-cookie/dist/js.cookie.mjs'
We're only providing a single default export.. not sure what I'm missing, but how would * work?
There's currently work in progress for v3, supporting ES modules right away, and what you're looking at might be the documentation at master (which doesn't apply to v2).
As the readme tries to advise, you should take a look at the documentation for the latest _release_, which is here: https://github.com/js-cookie/js-cookie/tree/latest#readme
v3 hasn't been released yet.
Sorry, I did NOT read that hint. If possible, create an index.js to export the internal dist file:
export { Cookie } from './dist/js.cookie.mjs';
So we can still use the following:
import Cookies from 'js-cookie';
Ah, thanks, that never seemed right to me, going to look into this..
Not sure how this is going to work! The idea was that in the package definition we have a browser property (instead of main) pointing to the UMD variant of this library, for maintaining full compatibility. And then there’s a module property, pointing to the ES module distribution — this is what both webpack and rollup advise going forward with ES modules until there’s going to be a standardized approach — so we’re supporting ES module in this way.
The index.js that you’re proposing would only work when used with main/browser?
Ah, no.. the index.js is supposed to sit in the root.
I never worked with release-it but you might use their hooks to create or copy the index.js to the root.
"hooks": {
"after:bump": "npm run dist-that-also-copies-index"
}
Let me know if you need a helpful hand or someone for testing.
In case you didn't know... just add 3.0.0-beta.1 to your package.json to release a unstable version. A badge like this will help to recognize as it changes the color according to the stability.
This could become useful: https://www.npmjs.com/package/create-index
Nice... I've set up a little webpack example, ~and using module in the package definition seems to be all we need.~
import Cookies from 'js-cookie'
works without adding another index.js file at all. I'm going to adapt the documentation and might also add the example to the repo.
..but because we‘re using both the browser and module properties in the package.json, webpack uses the former for bundling:
https://webpack.js.org/configuration/resolve/#resolvemainfields
Tested with the following webpack.config.js and it works the same, this time picking up the ES module dist file:
module.exports = {
resolve: {
mainFields: ['module', 'main']
}
}
@redaxmedia I've just released v3 as a beta, would be helpful if you find the time to try it out:
$ npm install --save js-cookie@beta
@carhartl thanks for your work. However, when using the lib (3.0.0-beta.1) outside a bundle, in my case in a jest test, i get the following error when importing it with:
import Cookies from 'js-cookie';
Cannot find module 'js-cookie' from 'Results-test.js
@symn Could you provide a repo or code that allows for reproducing the problem?
We might have to do this: https://github.com/js-cookie/js-cookie/issues/544#issuecomment-535440792
In any case I guess I will soon find out anyway: #573
@symn Could you try again with the latest beta? I've just bumped it and it might provide a fix for your problem.
@symn Could you try again with the latest beta? I've just bumped it and it might provide a fix for your problem.
Sorry for the late response (christmas holidays).
I tried version 3.0.0-beta.3 and it works now! Great, thx for your work!