Node.js SDK - Square.Client is not a constructor

Description:

I am trying to use the Square Node.js SDK to build an application that receives webhooks from Square. I am following the documentation, but I am getting an error that says “Square.Client is not a constructor”.

I have tried the following:

  • Installing the latest version of the Square Node.js SDK
  • Importing the Client and Environment objects directly
  • Accessing the Client and Environment objects directly from the Square module
  • Creating a new, minimal Node.js project and installing only the square package
  • Running a simple script that just initializes the Square.Client

However, I am still getting the same error.

Here is my code:

const Square = require(‘square’);

const client = new Square.Client({
accessToken: ‘YOUR_ACCESS_TOKEN’,
environment: Square.Environment.Sandbox,
});

console.log(client);

I am using Node.js v22.14.0.

Can anyone help me with this?

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Node.js SDK
Set up SDK for a Node.js Project
Stay Current with the Node.js SDK Version

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

The issue you’re encountering might be due to the way you’re importing the Square module. In the Square Node.js SDK, the Client and Environment objects are not directly accessible from the Square module. Instead, you should import them separately.

Here’s how you should structure your import statement and client initialization:

const { Client, Environment } = require('square');

const client = new Client({
  accessToken: 'YOUR_ACCESS_TOKEN',
  environment: Environment.Sandbox,
});

console.log(client);

In this code, the Client and Environment objects are destructured from the square module. Then, a new Client instance is created using your access token and the Environment.Sandbox environment.

This answer was reviewed by @Bryan-Square.

Thanks for the suggestion, but I’m still encountering the same error even when importing the Client and Environment objects separately.

Here’s the code I’m using:

const { Client, Environment } = require(‘square’);

console.log(Client); // Log the Client object
console.log(Environment); // Log the Environment object

const client = new Client({
accessToken: ‘YOUR_ACCESS_TOKEN’, // Replace with your actual access token
environment: Environment.Sandbox,
});

console.log(client);

And here’s the output I’m getting:

undefined undefined TypeError: Cannot read properties of undefined (reading 'Sandbox')

As you can see, both Client and Environment are undefined, and I’m still getting the “TypeError: Cannot read properties of undefined (reading ‘Sandbox’)” error.

I’ve tried this approach in both my original project and a clean project setup, but the error persists. I’m using Node.js v22.14.0.

Could there be another issue at play here? Any further guidance would be greatly appreciated.

What version of the SDK are you using? :slight_smile:

I am using [email protected]

If you try our quickstart example does it work:

const { SquareClient, SquareEnvironment, SquareError } = require("square");
require('dotenv').config()

const client = new SquareClient({
  token: process.env.SQUARE_ACCESS_TOKEN,
  environment: SquareEnvironment.Sandbox,
});

async function getLocations() {
  try {
    let listLocationsResponse = await client.locations.list();

    let locations = listLocationsResponse.locations;

    locations.forEach(function (location) {
      console.log(
        location.id + ": " +
        location.name + ", " +
        location.address.addressLine1 + ", " +
        location.address.locality
      );
    });
  } catch (error) {
    if (error instanceof SquareError) {
      error.errors.forEach(function (e) {
        console.log(e.category);
        console.log(e.code);
        console.log(e.detail);
      });
    } else {
      console.log("Unexpected error occurred: ", error);
    }
  }
};

getLocations();

:slight_smile: