How to put another js api library in cocos creator

Hi, I am just wondering how to put another js API library( in my case, i want to put the smartfoxserver javascript API library) in cocos creator? because what i did in cocos2d-js, i just need to add it in project.json, and I am wondering if i can do same way in cocos creator or another way?

thank you in advance :slight_smile:

If you find an easy answer to that please let me know :wink:

We added a few api so far but there was always some custom tweeks to do.

In general, just make sure the lib ends with :
module.exports = [SFS2X in your case];

Also make sure you uncheck “import as plugin” on the script in Creator.

One big challenge we had with that lib was to make it “strict”, since Cocos seems to add that to all javascript code you add to the project.

I’m pretty sure there’s some things I didn’t understand, even I succeeded in importing a few api.
I’d like to hear a more “official” answer as well! :slight_smile:

Hi, thank you for the reply. in which directory should i put the file there? i am trying my luck here as well. i still can’t figure it out where is the directory, and also the exact position of the module.exports = [SFS2X in your case]; itself haha

As long as it’s within the assets folder it can be found by cocos, though a designated script folder is recommended, module.exports=x; is added at the end of the script file outside of the script brackets.

var x = {
/*
your stuff
*/
};
module.exports = x;

cheers

Thanks for the help. I’m outlining here what i did, so that it may be helpful for others coming here.

I’m pretty new to Cocos Creator and after making my first game in it, I decided to implement the Gamejolt API JS library. Copied the gj-js-api-test.js file into the scripts folder first.

After the first line var GJAPI = {};
I added module.exports = GJAPI;

Now to access this (To get username, or to Post Score to Leaderboard) I searched and found this answer.

When I accessed the external script like var GJAPI = require(“GJAPI”); It didn’t work. Compile Error.

After some search found that file name is to be given (why? don’t know) without extension.

So I get the username by calling this function from another script (from Game Script).

var GJAPI = require("gj-js-api-test");
this.userLabel.string = "Welcome "+GJAPI.sUserName;

Hope this helps someone. Also You can Try My Game on Gamjolt

Also Made a Video on Youtube : How to Implement Gamejolt API in Cocos Creator

Please someone help me how can i add this library https://unpkg.com/mathjs@4.2.2/dist/math.js
to my cocos creator game ?