0

Trying to wrangle jQuery UI into a Typescript 2.6.2 project that already has jQuery working nicely, but running into a brick wall. Compiles fine but at runtime I get this:

Exception has occurred: ReferenceError

ReferenceError: jQuery is not defined

Here is my main.ts file:

import $ = require('jquery');
import "jqueryui";

$(document).ready(() =>
{
    $( "#slider" ).slider( {
        orientation: "vertical",
        min: 0,
        max: 150,
        value: 50
    } );
});

Here is the index.html file:

<!doctype html>
<html>
<head>
    <script src="./bundle.js"></script>
</head>
<body>
    <div id="slider"></div>
</body>
</html>

For reference, here are my tsconfig.json, package.json, and the command line I use to transpile to JavaScript:

tsconfig.json:

{
    "compilerOptions":
    {
        "target": "es2015",
        "module": "commonjs",
        "moduleResolution":"node"
    },
    "include":
    [
        "spec/*.ts"
    ]
}

package.json:

{
  "name": "tsbugger",
  "version": "0.0.1",
  "description": "debug config test",
  "main": "index.js",
  "scripts": {
    "test": "jasmine"
  },
  "repository": {
    "type": "git",
    "url": ".git"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/jasmine": "^2.8.6",
    "@types/jquery": "^3.3.0",
    "@types/jqueryui": "^1.12.2",
    "browserify": "^15.2.0",
    "jasmine": "^2.9.0",
    "jquery": "^3.3.1",
    "jqueryui": "^1.11.1",
    "tsify": "^3.0.4",
    "typescript": "^2.6.2"
  }
}

build command:

browserify main.ts --debug -p tsify 1>bundle.js

How can I get jQuery UI working in Typescript with proper typings?

1

1 Answer 1

1

Looks like jquery-ui supports only amd modules and global installation. So, try to change tsconfig.json to use module: 'amd', or just add jquery and jqueryui via <script> tag without import statement.

There code from jquery-ui.js

(function( factory ) {
if ( typeof define === "function" && define.amd ) {

    // AMD. Register as an anonymous module.
    define([ "jquery" ], factory );
} else {

    // Browser globals
    factory( jQuery );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.