Search code examples
typescriptnpmwebpackcommonjsts-loader

Webpack and ts-loader bundle returning TypeError on third party library


Everything compiles and bundles however there is a TypeError in the browser: "box2dweb_commonjs_1.default is undefined." No error on starting webpack-dev-server and checking the bundle at http://localhost:8080/webpack-dev-server/. The project is on GitHub https://github.com/paboulos/ts-loader_box2d What is wrong with the bundle?


Solution

  • box2dweb_commonjs_1.default i

    Common issue. The library doesn't export a default. Instead of using

    import box2dweb from "whatever"; 
    

    You need to use

    import * as box2dweb from "whatever"; 
    

    More

    The library doesn't export a default. Note that this would be a compile error if the library was written in TypeScript. People writing JavaScript are also confused about what is and what isn't a default export, so babel fixes it for them, however TypeScript doesn't.

    Long term bable way of automagically making the main export also the default export might win the standards choice but its not the case yet so TypeScript is punting.