How to authenticate using Javascript SDK?

Nakama server is running fine. Then I just created a add_user.js file and using npm and “npm i @heroiclabs/nakama-js” ran this command. No error and I can see heroiclabs folder in node_modules folder. Then I wrote the following code
`
import {Client} from “@heroiclabs/nakama-js”;

var useSSL = false; // Enable if server is run with an SSL certificate.
var client = new Client(“defaultkey”, “127.0.0.1”, “7350”, useSSL);
`

and getting the error “SyntaxError: Cannot use import statement outside a module”. Can anyone please help? How to run the JS codes in the right way actually?

In your package.json, add:

{ "type": "module" }

I didn’t create any package.json file. I just created one js file and wrote the codes there. Can you please help?

Try replacing import with require?

Now,
`
const { Client } = require(“@heroiclabs/nakama-js”);

var useSSL = false; // Enable if server is run with an SSL certificate.

var client = new Client(“defaultkey”, “127.0.0.1”, “7350”, useSSL);

I am not getting any error with the above code. But If I add the below code with the above one
var email = “super@heroes.com”;

var password = “batsignal”;

const session = client.authenticateEmail(email, password);
`
I am getting ReferenceError: XMLHttpRequest is not defined

What about something like:

const nakama = require("@heroiclabs/nakama-js");
const client = new nakama.Client(“defaultkey”, “127.0.0.1”, “7350”, false);

getting same error!

you may have missunderstood, that the documentation thinks you already have a running node/npm/yarn project set up.

You want to learn more about setting up a simple node application (with express.js for example) or react (create-react-app) or else.

There are also great videos to learn node.js