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'
not quite what your example asks for, but you can already do this:
import {v1 as uuidv1} from 'uuid';
or just
import {v1} from 'uuid';
if you're not picky about the accessor.
As noted in the README, importing uuid is deprecated. Instead, import uuid/<version>. E.g. to import the v1 generator using ES6 notation:
import uuidv1 from 'uuid/v1';
(Note: I haven't tested this, but give it a try and report back here with the results. I'll reopen if this is a legit issue.) Edit: Tested this. It works as expected with Webpack. If you're doing a bare import in the browser, you may have to add the .js extension.
@broofa import uuid from 'uuid/v4'; syntax does not work, at least in a Typescript v3 project running on Node v10.9.0 (without webpack nor babel, only ts-node to compile/run Typescript)
I get the following error: TypeError: v4_1.uuid is not a function
on the other hand, import {v4 as uuid} from 'uuid'; works as expected
(tested on uuid v3.3.2)
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...
at least in a Typescript v3 project
... 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 Typescript is handling CommonJS/ES6 imports
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
import uuid from 'uuid/v4';
const id = uuid();
@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
import { UUID } from 'uuid/v4';
export class entity {
id: string;
name: string;
constructor() {
this.id = UUID();
this.name = '';
}
}
results in
ERROR TypeError: "Object(...) is not a function"
But switching to
import UUID from 'uuid/v4';
fixed it.
Should be reopened and fixed.
Rollup is also giving error, with import uuidv4 from 'uuid/v4'. Plugins need to be used to fix it there and this's a single package I met which requires actions from developer to make imports work.
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 😉
Current index.js with exports is a mess
Agreed. Hence the deprecation warning about it going away.
FWIW, the problem with hanging all versions off the top-level module is that it it creates a dependency tree that causes packagers to pull in all 350-600 lines of code in this module, even in the very-common case where you only need the 30-50 lines required to implement v4.
Rollup is also giving error
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 index.js file it's not needed.
Top level exports from index.js file shouldn't be a problem because tree shaking is used to get only needed dependencies. Rollup and webpack both have it and should remove unused functions from bundle. So, if you only import v4 function, all other functions should be removed.
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
Package found! However, no "module" entrypoint was found in its package.json manifest. If possible, explore the site for a more web-friendly alternative.
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
https://www.npmjs.com/package/@bundled-es-modules/uuid
Cheers
Sorry for taking such a long time but we have finally released [email protected] which features native ESModules-Support for the browser:
npm install [email protected]
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 uuid without TS, so I don't know how to make sure your ESModule configuration works using vanilla JS)
anyway, both versions (3.4.0 and 7.0.0-beta.0) seem to work fine with Typescript using this syntax:
import uuid from "uuid/v4"
as long as you:
"@types/uuid": "3.4.7""esModuleInterop": true is set in your tsconfig file@Oliboy50 thanks for trying it out and posting the link to the sandbox!
I want to highlight that the new recommended way of importing is now:
import { v4 as uuidv4 } from "uuid";
document.getElementById("app")!.innerHTML = `<h1>${uuidv4()}</h1>`;
I quickly tried it out with your sandbox and it seems to work as well! Can you confirm that?
Oh, and @Oliboy50 if I'm not totally mistaken you can drop "esModuleInterop": false from your tsconfig if you use the new way of importing the library!
We have just released [email protected] which now features native ES6 import support. Please feel free to open a new issue if you encounter any issues with the new version.
I get a syntax error when importing uuid
SyntaxError: The requested module 'uuid' does not provide an export named 'v4'
@dps910 Which version do you see in your node_modules?
@dps910 Which version do you see in your node_modules?
version 7.0.2
this is how I imported
import { v4 as uuidv4 } from "uuid";
also my nodejs version v13.11.0
Ah, it's node. Only bundlers esm is supported for now.
https://github.com/uuidjs/uuid/pull/402
Can you try this?
import uuid from 'uuid';
uuid.v4
Ah, it's node. Only bundlers esm is supported for now.
402
Can you try this?
import uuid from 'uuid';
uuid.v4
thanks, it works :smile:
Ah, it's node. Only bundlers esm is supported for now.
402
Can you try this?
import uuid from 'uuid';
uuid.v4
WARNING in ./src/server/object.js 204:14-18
"export 'default' (imported as 'uuid') was not found in 'uuid'
Yes, it's the latest node trying to use 'import'
The alternative import v4 from "uuid"
v4() seems to run with warnings as well
@FloodGames I'm not sure what you are trying to say.
Per the docs the recommended way of using uuid as an ES Module is:
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'
For everyone in this thread: we have just released [email protected] which comes with native ESM support for Node.js! Let us know if that works for you. See CHANGELOG for more info.
@FloodGames @dps910 this means that the way to go from now on is:
import { v4 as uuidv4 } from 'uuid';
uuidv4();
For everyone in this thread: we have just released
[email protected]which comes with native ESM support for Node.js! Let us know if that works for you. See CHANGELOG for more info.@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.
Trying to test a file where I import uuid results with an error:
● Test suite failed to run
SyntaxError: The requested module 'uuid' does not provide an export named 'v1'
at async Promise.all (index 0)
at jasmine2 (node_modules/jest-jasmine2/build/index.js:228:5)
I saw @dps910 comment and I guess it's something similar.
I'm using UUID@8, node@14 and Jest@26 (and it's freshly-supported esm modules implementation).
Jest command:
node --experimental-vm-modules .\node_modules\jest\bin\jest.js
The file that I'm testing imports uuid like you suggested:
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)
import {v4 as uuid} from "uuid"
use now the function
uuid()
This compiled to typescript doesn't work:
import { v4 } from 'uuid';
v4();
Error:
Error: Package exports for '........ /node_modules/uuid' do not define a valid '.' target
I had to downgrade to v7
const { v4: uuidv4 } = require( 'uuid' );
console.log ( uuidv4( ) )
Angular compilation warning: application.service.ts depends on 'uuid'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
Most helpful comment
@broofa
import uuid from 'uuid/v4';syntax does not work, at least in aTypescript v3project running onNode v10.9.0(without webpack nor babel, onlyts-nodeto compile/run Typescript)I get the following error:
TypeError: v4_1.uuid is not a functionon the other hand,
import {v4 as uuid} from 'uuid';works as expected(tested on
uuid v3.3.2)