Package.json & typings for typescript (lodash, moment, etc)

I would like to use typescript with third party modules. And I am talking on the sample typescript project which Cocos Creator makes by default. For example, I want to add lodash, moment, etc

What is workflow to add these into my project.

  1. npm init (inside root)
  2. npm i --save lodash
  3. npm i --save-dev @types/lodash
  4. ??? // hidden step
  5. import * as _ from “lodash”; // inside my ts files

How to import from node_modules, which config to change? How to add types which were installed? Any small tutorial/sequences of commands is much appreciated.

1 Like

Question is closed, there is no need for step 4!

Does these modules work in native platform too?
Could you elaborate

Yes, I just created an empty “Hello-typescript” project
Added the steps above (plus installed moment: npm i --save moment), added this piece of code to ChildClass.ts:

import * as moment from "moment";
import * as _ from "lodash";
@ccclass
export default class ChildClass extends SuperClass {
    protected async testAsync(): Promise<string> {
        return new Promise<string>((resolve, reject) => {
            setTimeout(() => {
                let formats = [
                    "DD.MM.YYYY ddd HH:mm",
                    "DD.MM.YYYY",
                    "HH:mm"
                ];
                let str = "";
                _.forEach(formats, format => {
                    let time = moment().format(format);
                    str += "\n" + time;
                });
                resolve("Hello, World! From ChildClass!" + str);
            }, 1000);
        });
    }
}

and compiled. Exe file executes, no problem and show what is expected.

1 Like

Same for Android. Works perfectly!

I will try that. Thanks man.

With node.js packages, implementing many features becomes much easier… that is a great gain for cocos2d.

Now I plan to search for good dependency injector for services and etc.

how’d you got around the compile error in cocos due to same fileNames used for scripts in npm modules in different folder?

for eg: after removing functional programming lodash extension,still :

Compile error: Filename conflict, the module “toString” both defined in “Script\node_modules\lodash\toString.js” and “function toString() { [native code] }”

I did not have such error. How do you add modules, do you use --save to package.json and how do import modules to the project? May be I did not use toString or did not use method imports, I import whole lodash module as a one unit:

import * as _ from “lodash”;

I am using vanilla js(not ts), and i imported same way, in project ran npm init & npm i --save lodash.

and then using require in js script i use lodash.

However after changing some names which were conflicting and solving dependencies, creator always take huge ton of time to compile the scripts every time. Does that happen with you too?

No, nothing like that. Could be that typescript compile/packaging differently, or because it uses commonjs for modules?