javascriptnode-modulesuuid

I want to use the uuid generator but I have an error


I import this on my JS file :

import {v4 as uuidv4} from './uuid';

and here is the error :

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

need help pls


Solution

  • Check your imports because it looks like you're not doing it right.

    Create a UUID (ES6 module syntax):

    import { v4 as uuidv4 } from 'uuid';
    uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
    

    ... or using CommonJS syntax:

    const { v4: uuidv4 } = require('uuid');
    uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
    

    You can read more about it in npm.