Search code examples
javascripttypescriptbrowserifytsify

Browserify global variable is not found in the browser


I am writing a script in TypeScript, and when I use browserify, to convert it, my master variable is not found in the browser.

main.ts

import { GameSmart } from './GameSmart';

const gamesmart = new GameSmart();

gulpfile.js

/* require's snipped */

gulp.task('build', function () {
    return browserify()
        .add('./src/gamesmart/main.ts')
        .plugin(tsify)
        .bundle()
        .on('error', function (error) {
            console.error(error);
            throw error;
        })
        .pipe(source('gamesmart.js'))
        .pipe(gulp.dest('build/'));
});

In my page I am including the newly created file, and attempting to call the gamesmart variable, but it is not found.

<script scr="/path/to/sdk.js">
<script>
    gamesmart.game.started();
</script>

How can I make it as a global/root variable?


Solution

  • I was able to fix this by adding the variable to the window.

    window['gamesmart'] = new GameSmart();