Home > Software design >  Why can't I import jquery?
Why can't I import jquery?

Time:11-25

I am trying to use jquery to get a random fact. However I got alot of errors whilst trying to use ajax, after changing the way i defined $ or jquery alot of times it still hasn't solved it. I found another post with a similar problem and told me to use this:

var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;

var $ = jQuery = require('jquery')(window);

but this gives the error: Error: Cannot find module 'jsdom'

My package.json (the part with tyhe dependencies) looks like this:

"dependencies": {
    "quick.db": "^9.0.6",
    "jquery": "^3.6.1",
    "jsdom": "^20.0.3",
    "jsdom-global": "^3.0.2",
    "xmlhttprequest": "^1.8.0"
  }

Why does it keep saying it cannot find the module? Or should I use another way to request the data?

This is what I am trying to request by the way:

var limit = 1;
        
        $.ajax({
            method: 'GET',
            url: 'https://api.api-ninjas.com/v1/facts?limit='   limit,
            headers: { 'X-Api-Key': 'MY-API-KEY'},
            contentType: 'application/json',
            success: function(result) {
                console.log(result);
                interaction.reply({embeds: [
                    new EmbedBuilder()
                        .setTitle(`Random fact for @<${interaction.user.id}>`)
                        .setAuthor({name: 'vixitu', iconURL: 'https://external-preview.redd.it/lqDFDXXvfqMs7kyQ9y1FrGcQzdCE23uMPlcxFqo_oYE.png?width=640&crop=smart&auto=webp&s=1aad996b62437feb367356c6bed434385bda0699'})
                        .setDescription("Here is your random fact.")
                        .setThumbnail('https://external-preview.redd.it/lqDFDXXvfqMs7kyQ9y1FrGcQzdCE23uMPlcxFqo_oYE.png?width=640&crop=smart&auto=webp&s=1aad996b62437feb367356c6bed434385bda0699')
                        .addFields(
                            {name: 'Random fact #1', value: result}
                        )
                        .setTimeStamp()
                ]})
            },
            error: function ajaxError(jqXHR) {
                console.error('Error: ', jqXHR.responseText);
            }
        })
        
    },

CodePudding user response:

Please check whether jsdom folder exists in the node_modules folder.

In addition, I recommend you describe how to install the jsdom module, such as npm or yarn.

CodePudding user response:

I guess you should install it

npm install jquery --save 

and then import it on top of you page

import 'jquery/dist/jquery.min.js';

check this out https://www.npmjs.com/package/jquery

  • Related