thunderclient / thunder-client-support

Thunder Client is a lightweight Rest API Client Extension for VS Code.
https://www.thunderclient.com
Other
3.62k stars 127 forks source link

Import Node library 'moment' does not work #1585

Closed BrunoLaporte closed 1 month ago

BrunoLaporte commented 1 month ago

Describe the bug I'm having trouble importing a Node library in a folder inline script. It is worth noting I just got a paid licence through my organization before trying this.

I have this error when running a query : Error in Pre Request Script: - moment is not a function

Here's the code :

var isLocalRequest = tc.getVar("isLocalRequest") == true

if (isLocalRequest) {
    tc.setVar("ccm", tc.getVar("local-variable-1"));
    tc.setVar("docclient", tc.getVar("local-variable-2"));
}
else
{
  tc.setVar("ccm", tc.getVar("variable-1-url"));
  tc.setVar("docclient", tc.getVar("variable-2-url"));
}
var moment = await tc.loadModule("moment");

const datesFormat = [
    {
        name: "generation-date", 
        format: "YYYY/MM/DD"
    },{
        name: "generation-datetime", 
        format: "YYYY/MM/DD HH:mm:ss"
    },{
        name: "timestamp", 
        format: "YYYYMMDDHHmmSS"
    }
]

datesFormat.map(date => {
    tc.setVar(date.name, moment().format(date.format))
})

Please let me know what I'm doing wrong I followed the documentation here : https://docs.thunderclient.com/scripting/libraries

Thanks in advance

To Reproduce Add code as above in folder pre-request script Launch request in said folder get error

Expected behavior I expected moment to work and format the date/time

Platform:

Are you using the free version/paid version/trial: paid

rangav commented 1 month ago

@BrunoLaporte We will verify the code and get back to you

rangav commented 1 month ago

I have verified the script, The code is working for me.

Please update the extension to the latest version - 2.24.13 and try again

BrunoLaporte commented 1 month ago

Just updated my environment vscode version: 1.92.0 node version: 20.14.0 extension version: v2.24.13

Same error. I have the pre-request script configured in the yellow folder on the image if this changes anything for the request.

image

BrunoLaporte commented 1 month ago

FYI, my collection and environments were erased when I activated the git sync feature. I started from scratch creating my same request and environments. I still have the same error.

rangav commented 1 month ago

You need to copy existing data from default location to git project See Note 3 in below link https://docs.thunderclient.com/team

Node modules try running below code and share logs

console.log("load node module");

var moment = await tc.loadModule("moment");

// use moment specific version
var moment = await tc.loadModule("moment", "2.30.0");

console.log(moment().format());
BrunoLaporte commented 1 month ago

I reinstalled node on WSL and it works now !