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,
});
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:
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:
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.
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.