Best way to use Node.js modules with Cocos2d-js

Hello Everyone,

I’m new to Cocos2d-js and have been trying to use Node.js modules within my project, but have come across some challenges.

I’ve created the node_modules directory (with the correct modules installed within it) and the package.json file and placed these within the src folder containing app.js and resource.js files.

Now when i try to instantiate the module within app.js:

var Module = require(‘module’);
var module = new Module();

I get a “Module is not a constructor” error.

I feel it has something to do with the “project.json” file and having this node_modules folder included in there but i’m not sure how that should be done.

Any help on this would be greatly appreciated!

1 Like

If you are using Cocos2d-js template to create project (I guess YES) than consider one thing: default app.js is client side javascript and what you are trying to do is to mix node.js which is server side javascript into client side. Running node scripts requires node installed on your machine. How it suppose to work on client’s browser ? For the simple answer: included node.js module will not be working on client side.

2 Likes

Thank you for the response!

This makes sense and I’m looking into some client side alternatives.

What functionalities do You need ? …and the second thing If we back to the topic about more js libs in your project it depends on which platform you want to hit because it’s better to use more cocos2d-js functions than other libs because cocos2d-js binds to Cocos2d-X and then it’s almost native speed, if you want to release your project on mobile devices, in the other hand if the platform will be only browser - it’s OK.

1 Like

Hey kowciany,

I am looking to use an ORM to store client side data for games made on cocos2d-js. As far as I know, cocos2d-js currently doesn’t have this type of support which is why i’m looking for third party javascript libraries that I could plug in. Several of the current popular javascript ORM’s are modules of Node.js, which is why I was hoping I could plug them into a cocos2d-js project.

I am currently looking into javascript ORM’s that work on client side databases instead of a central server side database like JayData (http://jaydata.org/), and I have the data storing on the client when the game is run in a browser using cocos2d-js, but I am experiencing some issues with getting the data to store on the client when I run the game as a native IOS/Android app.

Do you have any experience with ORM’s and cocos2d-js?

Sorry, I didn’t use any ORM on cocos2d-js 3.0. I’ve just hitted server over ajax to get data. But if you need only store data not the complex ORM you can try local storage. I didn’t test it but I know there is a local storage in cocos2d-js 3.0 on cc.sys.localStorage it was introduced in cocos2d-html5 (it now part of cocos2d-js) in 2.x.x as sys.localStorage or something because there are test here -> http://www.cocos2d-x.org/html5-samples/samples/tests/index.html -> slide down menu after loading and click Sys tests then open browser console to see the result… In this document I’ve found that it’s still exists but was renamed from sys.localStorage to cc.sys.localStorage http://cocos2d-x.org/docs/manual/framework/html5/v3/cc-sys/en and it’s also available in cocos2d-x but there is no documentation http://www.cocos2d-x.org/reference/native-cpp/V3.2/dir_35c73a41342940d60c5e1e1b9246f8d5.html but hopefully it can work not only on browser but with jsbindings too. If somebody know something about this please update or correct me.
Anyway for the sample of local storage from 2.2.3:

    var key = 'key_' + Math.random();
    var ls = sys.localStorage;
    cc.log(1);
    ls.setItem(key, "Hello world");

    cc.log(2);
    var r = ls.getItem(key);
    cc.log(r);

    cc.log(3);
    ls.removeItem(key);

    cc.log(4);
    r = ls.getItem(key);
    cc.log(r);
enter preformatted text here

So change sys to cc.sys and try :smile:

Additionally if you want to save more than one string or number you can JSON.stringify the result of object literal to the key.

Only pure JavaScript node modules can be used in JSB, anything related to a server implementation won’t work. For using those modules, you can refer to http://browserify.org/

For client storage, I think it’s better to use C++ lib sql and bind the API to javascript

OK. Thx for response @pandamicro but if we use c++ solution how can it work on browser and Cocos2d-HTML5 ?

On web, One solution is to fallback to Web SQL.

What IDE are you using to code node.js?
Cocos Code IDE or WebStorm or others?

FYI, our team has open-sourced a couple of repositories to help others get started with cocos2d-html, webpack, babel, NPM, etc.

Project boilerplate: https://github.com/curriculum-advantage/cocos2d-html5-boilerplate

Utilities (boilerplate has this installed already): https://github.com/curriculum-advantage/coconut