Its possible to add another javascript library to CocosCreator?

How I add another javascript file that is a library to my game ?
I trying to add http://mathjs.org library putting the js file in the scripts folder and calling with:

var Math = require("Libraries/math.js");

but Cocos Creator says:
"load script [Libraries/math.js] failed : Error: Cannot find module ‘Libraries/math.js’ "

Anyone can give me some direction? Please

1 Like

http://www.cocos2d-x.org/docs/creator/manual/en/scripting/plugin-scripts.html

1 Like

BitsBR - I’ve been trying to figure out how Cocos creator does this all day. And in the end… it’s really easy.

In your project assets make a “scripts” folder (just for organization)
Drag the math.js file to this folder so it’s copied into the project.
Select the math.js file in the asset list.
In the property inspector (top right side), check “import as plug-in”
Click tiny check box on top of property inspector to save this setting (soooo easy to forget this one)
And…that’s it.

No need for “require” or anything else. Use your math functions in Creator based javascript as you need.
Tadah!

Be well

2 Likes

You are F**ked. I have not found a good way of importing libraries into cocos creator, thats why js is still play toy. Try importing as plugin and if it doesn’t work, you have to read the source code of math.js and figure out a way to copy and paste those functions into your scripts. Have fun

1 Like

I know how to do it for typescript. But that should not be different from JS, right?

  1. inside project make npm init and create empty package.json with that (or create the file by hand)
  2. npm i mathjs
  3. var math = require('mathjs');
1 Like

Thank you for all your help friends, all you are all awesome!
it’s working, but if somebody use the Production version (“math.min.js”),its also needed to add one file called “math.min.map”, I find it in the npm module.