Hi @Rick_Wallace,
What version of the node-asana client library are you using? This will help us troubleshoot your issue. Assuming you installed the library with npm
can you run the npm list
command in your terminal. It should show you something like:
├── asana@3.0.0
ERROR 1: TypeError: Cannot read properties of undefined (reading 'Client')
I see that the code snippet you used when you encountered this error was:
import asana from 'asana';
const client = asana.Client.create().useAccessToken('my_token');
This code sample is from the v1.X.X version of the node-asana client library. The reason why you encountered this error is because in node-asana v3.X.X the way that you initialize the client is different and it does not have a Client
class hence why you received the error TypeError: Cannot read properties of undefined (reading 'Client')
SOLUTION: Utilize the appropriate sample code. You can view the sample code for the function you want to use either via:
OPTION 1: Developer Docs
OR
OPTION 2: Reference the README.md
on GitHub repo of the specific version you are using
NOTE: for node-asana v1.X.X there you can view the sample code in the /samples
folder of the repo
For context:
This is how you would initialize the asana client with the new node-asana client library (v3.X.X)
const Asana = require('asana');
let client = Asana.ApiClient.instance;
let token = client.authentications['token'];
token.accessToken = '<YOUR_ACCESS_TOKEN>';
ERROR 2: ReferenceError: process is not defined
I’ll need more information about which version of the client library you are using to get a better understanding.
It looks like in this scenario you are trying to export a function that calls the Asana client to make requests. I’ve noticed that you switched between import asana from 'asana';
and const Asana = require('asana');
I am assuming you are aware of the difference between the import
statement and require
statement. As a note for future folks out there, if you would like to use import asana from 'asana';
you’ll need to add "type": "module"
to your package.json
Here’s an example of how this setup works:
SCENARIO 1: Using import
Files in your working directory:
package.json
helper.js
main.js
package.json
{
"type": "module"
}
helper.js
import Asana from 'asana';
let client = Asana.ApiClient.instance;
let token = client.authentications['token'];
token.accessToken = "<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>";
let usersApiInstance = new Asana.UsersApi();
export const getUser = async () => {
let user_gid = 'me';
let opts = {};
return await usersApiInstance.getUser(user_gid, opts);
};
main.js
import { getUser } from "./helper.js";
console.log(await getUser());
SCENARIO 2: Using require
Files in your working directory:
helper.js
main.js
helper.js
const Asana = require('asana');
let client = Asana.ApiClient.instance;
let token = client.authentications['token'];
token.accessToken = "<YOUR_ASANA_PERSONAL_ACCESS_TOKEN>";
let usersApiInstance = new Asana.UsersApi();
const getUser = async () => {
let user_gid = 'me';
let opts = {};
return await usersApiInstance.getUser(user_gid, opts);
};
module.exports = {getUser}
main.js
const users = require("./helper.js");
const main = async () => {
let response = await users.getUser();
console.log(response);
}
main();