-
-
Notifications
You must be signed in to change notification settings - Fork 900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6 import support? #245
Comments
not quite what your example asks for, but you can already do this:
or just
if you're not picky about the accessor. |
As noted in the README, importing
Edit: Recommended incantation is now: import {v1 as uuidv1} from 'uuid'; |
@broofa I get the following error: on the other hand, (tested on |
Apologies for the late response on this. I [still] consider this issue closed, but in light of the "reactions" the above comment has garnered, let me state my position on this...
... which makes this a Typescript (or TS configuration) issue. This module conforms with the requirements for CommonJS module systems. ES6 syntax should work with packagers that know how to transpile CommonJS modules to ES6. |
@broofa actually you're right, in my case this was only due to how so, just like many other JS libs like this one, it works this way using TS: import * as uuid from 'uuid/v4';
console.log(uuid()); here is a codesandbox to play with: https://codesandbox.io/s/5372omm68n |
This too works
|
@Sampath-Lokuge that doesn't work in Typescript. |
@jonathanstiansen What was the error? I have used above on Ionic / Typescript app and no issues. |
Typescript 3.2.4 in angular 7.2.7 Creating an object from this class
results in
But switching to |
Should be reopened and fixed. Rollup is also giving error, with Current index.js with exports is a mess and should be changed to a correct one so we don't need to import from separate files. Package.json file also doesn't reflect needed files. Es modules support would be also great since it's actually quite easy to add and maintain using modern tools. @broofa if you are okay with this, PR's can be made to reflect this changes as this package clearly needs some fresh updates to it 😉 |
Agreed. Hence the deprecation warning about it going away. FWIW, the problem with hanging all versions off the top-level
What's the Rollup team have to say about this? |
@broofa rollup team is pushing forward es modules, for a very long time. To get rid of such cjs errors, a plugin (rollup-plugin-commonjs) should be used there, but with top imports from Top level exports from |
tree-shaking only works on ES6 modules (according to webpack). |
@broofa yep, this is why I am proposing to add es modules to this package. Bundlers (webpack, rollup) will use es modules and tree shake unused exports while cjs users will have a separate cjs version. This is how a lot of modern packages work nowadays :) |
I recommend using pika pack, it will generate Deno, browser, and Node compatible versions https://next.pikapkg.com/blog/introducing-pika-pack/ |
Any ETA on this? |
@cannjeff: There's no ETA. If your packager doesn't support ES6-style import of CommonJS modules like this one, then you need to take that up with the packager project. |
Would you accept a PR that created different distribution types? |
@arjunyel: Short answer? No, not at this time. Longer answer: I know this is causing people pain, and I genuinely feel bad about not being responsive to that. However, to the best of my knowledge this module complies with the CommonJS spec, which makes this an issue with how packagers support CommonJS -> ES6 transpiling. Adding extra complexity to this project to create packager-specific distributions (which are just going to get repackaged/transpiled a second time anyhow) is not something I'm interested in. I'm open to continuing this conversation, but only to the extent you or others (or, ideally, the Rollup/Angular/Typescript teams) make a compelling argument for why this problem should be fixed here rather than in the packagers. To date, I haven't seen that effort being made. |
I feel like his may be relevant to the discussion here: https://jasonformat.com/enabling-modern-js-on-npm/ and will hopefully make you reconsider 😊 |
I came to this issue after going from https://www.pika.dev/search?q=uuid
I believe as patterns like this get adopted, ES module is going to be directly used in the browser instead of being repackaged or transpiled away. |
We've published a mirror of this repo which is bundled as ESM. https://github.com/bundled-es-modules/uuid Cheers |
Sorry for taking such a long time but we have finally released
The master branch README already reflects the API of v7.0.0. It would be awesome to get feedback from you if this release works well for you. |
To try your different uuid releases using Typescript, you can use this sandbox if you want: https://codesandbox.io/s/gallant-voice-iwy7z (I personnally didn't try to use anyway, both versions ( import uuid from "uuid/v4" as long as you:
|
I get a syntax error when importing uuid |
@Dps910 Which version do you see in your node_modules? |
version 7.0.2
also my nodejs version |
Ah, it's node. Only bundlers esm is supported for now. Can you try this? |
thanks, it works 😄 |
WARNING in ./src/server/object.js 204:14-18 Yes, it's the latest node trying to use 'import' The alternative import v4 from "uuid" |
@FloodGames I'm not sure what you are trying to say. Per the docs the recommended way of using import { v4 as uuidv4 } from 'uuid';
uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d' Does this not work for you? |
Oh, I think I lost something in my previous answer. This does indeed currently not work in blank nodeNode.js. Running Node.js 14.0.0 I can do the following: import uuid from 'uuid';
console.log(uuid.v4()); // -> 'b80a7443-e722-4877-be96-4f608304ca81' |
BREAKING CHANGE: Native ES Modules is still an experimental API in Node.js 14.0.0 and has so far not officially been supported by the `uuid` module. Since Node.js allows importing CommonJS modules it was possible to import the `uuid` module like this: ```js import uuid from 'uuid'; console.log(uuid.v4()); // -> 'cd6c3b08-0adc-4f4b-a6ef-36087a1c9869' ``` This will no longer work with proper ES Module exports in place. You can now import the `uuid` library as described in the documentation: ```js import { v4 as uuidv4 } from 'uuid'; uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d' ``` or ```js import * as uuid from 'uuid'; console.log(uuid.v4()); // -> 'cd6c3b08-0adc-4f4b-a6ef-36087a1c9869' ``` Enabling native ES Modules for Node.js requires some special care for the v1 algorithm which needs internal state. This makes this library susceptible to the dual package hazard described in https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_dual_commonjs_es_module_packages While the "isolated state" solution seems to make more sense it causes trouble with rollup which supports CommonJS files only with an additional plugin, see rollup/rollup#3514. It is worth noting that webpack could deal with the "isolated state" solution since webpack supports CommonJS sources out of the box without further plugins and also doesn't get confused by `.cjs` file extensions that would have to be used in the state isolation approach for compatibility with Node.js. The wrapper approach should however work fine. Here's what code will be used in each case: 1. Node.js `require('uuid')` -> dist/index.js (CommonJS) -> dist/v1.js (CommonJS) 2. Node.js `import { v1 as uuidv1 } from 'uuid'` -> wrapper.mjs (ESM) -> dist/v1.js (CommonJS) 3. rollup/webpack (targeting Node.js environments) -> dist/esm-node/index.js (ESM) -> dist/esm-node/v1.js (ESM) 4. rollup/webpack (targeting Browser environments) -> dist/esm-browser/index.js (ESM) -> dist/esm-browser/v1.js (ESM) Fixes #245 Fixes #419 Fixes #342
For everyone in this thread: we have just released @FloodGames @Dps910 this means that the way to go from now on is: import { v4 as uuidv4 } from 'uuid';
uuidv4(); |
Yes, it is working thank you :) |
Hi, I'm using jest to test my project.
I saw @Dps910 comment and I guess it's something similar. Jest command: import { v1 as uuidv1 } from 'uuid';
uuidv1(); It fails only when I'm testing though. |
Had the same issue, this worked in fixing the import: Import correct version (v1-4)
use now the function
|
This compiled to typescript doesn't work:
Error: I had to downgrade to v7 |
const { v4: uuidv4 } = require( 'uuid' ); |
Angular compilation warning: |
Maybe this could be useful to someone. In a Vue application (Vue 3 with typescript) I call uuid this way:
To make it works I have to change the import to: |
@crystalfp: This package does not supply a top level index.js. If you look at our package.json, you'll see that the |
Locking this conversation. Anyone having issues with importing |
Hey y'all,
Thanks for your work on this lib. Any plans for ES6 import support in the near future? If it's something actively being worked on I'm gonna wait, otherwise I may look into possibly PRing?
EG
import { uuidv1 } from 'node-uuid'
The text was updated successfully, but these errors were encountered: