Search code examples
javascriptcocos2d-xcocos2d-js

Cannot start cocos after adding a new file


I have a bug where I'm met with the error: "Uncaught TypeError: Cannot read property 'style' of null" When running the command cocos run -p web. This results in not being able to start my app.

I seem to get this error when I add a new js file to my source folder. I've solved it previously by restarting my terminal, which seemed to do the trick. However, now I am unable to run my app in the browser because of this error.

The full error log can be seen in the below image: enter image description here

It seems like the canvas element in CCBoot.js is null due to something not loading correctly. How can I go about solving this problem?


Solution

  • Eventually solved this after around 4 hours of debugging... The solution was painfully trivial, but the warning messages cocos2d-x was producing were extremely unuseful.

    The problem stemmed from not being able to parse the project.json file correctly. When I added my new file to the jsList array in project.json, I added it like this:

    {
        "project_type": "javascript",
    
        "debugMode" : 1,
        "showFPS" : true,
        "frameRate" : 60,
        "noCache" : false,
        "id" : "gameCanvas",
        "renderMode" : 0,
        "engineDir":"frameworks/cocos2d-html5",
    
        "modules" : ["cocos2d"],
    
        "jsList" : [
            "src/resource.js",
            "src/app.js",
            "src/newFile.js",
        ],
    }
    

    The comma after src/newFile.js and the jsList array messed up the file, causing all the problems. Therefore, to fix the project.json file I changed it to:

    "jsList" : [
        "src/resource.js",
        "src/app.js",
        "src/newFile.js"
    ]
    

    To conclude, if you are also experiencing problems like this, check the formatting of your project.json file and make sure everything is formatted correctly - if you're unsure how to format it start a new project and use a fresh project.json file as a guide!