Step-by-step guide on integrating jQuery npm module into TypeScript 1.8

Initially, I am struggling to articulate the correct question. It seems like there may be some confusion or overlap in my understanding of the TypeScript compiler, new ES2015 (ES6) module syntax, and module loaders.

My current objective is to utilize ES2015 module syntax to import an npm-installed jquery module while ensuring that TypeScript transpiles it to ES5.

However, upon attempting to open index.html, I encountered an error:

system-polyfills.src.js:1276 Error: SyntaxError: Unexpected token <
Evaluating http://localhost:3000/jquery
Error loading http://localhost:3000/app/main.js

The content of index.html is as follows:

<html>
  <head>
    <title>Bare TypeScript</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">    
    <link rel="stylesheet" href="styles.css">
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/jquery/dist/jquery.js"></script>
    <script>
      System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>
  </head>
  <body>
    <div>Loading...</div>
  </body>
</html>
  • I'm uncertain about the necessity of
    <script src="node_modules/jquery/dist/jquery.js"></script>
    .
  • Why does the B import function correctly even when console.log($) is disabled? Shouldn't the jQuery import also function properly?
  • Could the issue be related to mixing npm's commonjs modules with systemjs?

In my main.ts file, the code appears as follows:

import * as $ from 'jquery'
import B from './app.b'

console.log("Monkey");
let b = new B();
console.log($);

How can I configure tsc to effectively work with ES2015 module syntax? Please note that I am using TypeScript version 1.8.

The tsconfig.json file contains the following settings:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

Lastly, the package.json file lists the dependencies as:

"dependencies": {
    "es6-shim": "^0.35.0",
    "jquery": "^2.2.2",
    "systemjs": "0.19.25"
  }

The typings.json file includes the following ambient dependencies:

{
  "ambientDependencies": {
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
    "jquery": "registry:dt/jquery#1.10.0+20160316155526"
  }
}

Answer №1

Attempting to import jQuery as a systemjs module may result in compatibility issues since it is AMD based. Furthermore, if your app.b does not have a default export, the TypeScript compiler will generate an error. Have you verified for any compiler errors?

You can refer to this Github link for more information on loading jQuery using ES6 import syntax.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with running npm start in the latest version of React

After working on React 17.0.2 for a while, I recently made the switch to React 18.0.2. However, I encountered a issue when trying to run 'npm start'. Despite attempting various solutions such as installing react-scripts, running npm i, and uninst ...

Issue: Module 'zeparser' not found

My node.js installation is complete and I have successfully created the file. var http = require('http'); var url=require('url'); var fs=require('fs'); var io = require('socket.io'); http.createServer(function (req ...

Encountered a problem while trying to 'npm install' an Ionic 2 application

Encountered an error while running 'npm install', seeking assistance. I faced this issue when trying to npm install an Ionic 2 app from a project completed about a year ago, but encountered no issues when building a new app using ionic start. Ine ...

Exploring the power of Supabase's two-tiered joins using TypeScript

After reviewing the documentation here, I managed to successfully implement the first level join (agent_profile) but encountered issues when trying to join the next level (agent_office). Although the query returns the correct data, both VS Code and my app ...

I recently updated all my projects to Angular 14, but when I tried to install a package using `npm i`, it still

The challenge at hand I encountered an issue with my two Angular projects. The first project serves as a library utilized by the second project. I recently upgraded both projects to Angular 14 following this guide. However, after running an npm i on the ...

Sending data from the LoginComponent to the RootComponent

I am facing a challenge with implementing *ngIf to hide the login/logout option in the navbar based on the user's authentication status. When logged in, I want to hide the logout link. Here is my current setup. app.component.ts import { Component, O ...

When I delete the initial element from the array, the thumbnail image disappears

Using react-dropzone, I am attempting to implement image drag and drop functionality. The dropped image is stored in the React state within a files array. However, a problem arises when removing an image from the array causing the thumbnails of the remain ...

Whenever I attempt to run my script, the console on replit fails to function properly

As a complete beginner to Javascript, I'm having trouble getting my script to respond when I try to execute it. It just moves on to the next line and waits for me to type 'node index.js' again. I've included 2 images in an Imgur album - ...

Whenever I attempt to execute a command in cmd, this error consistently pops up

npm ERR! Windows_NT 10.0.18363 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\ADMIN\\AppData\\Roaming\\npm\\node_modules\\np ...

Tips for successfully interacting with dynamic text areas using Protractor:

My current project involves e2e testing for an Angular 4 application with Protractor. Despite my efforts, I am struggling to locate a unique id, class, name or text identifier for a specific textarea within the application code. I need assistance in find ...

"Received 'grunt command not found' error even though I have successfully installed grunt and

Even after installing grunt and grunt cli, I am still encountering a 'command not found' error. { "name": "angulartdd", "version": "1.0.0", "description": "1", "main": "index.js", "scripts": { "test": "echo \"Error: no test sp ...

Problem with Grouping of Columns in Material-UI

Having some trouble with column grouping in MUI data grid pro. I am using typescript and trying to implement column grouping, but encountering issues with the module GridColumnGroupingModel, which is used as the type definition for columnGroupingModel. Fol ...

What are the steps to resolve the `Error: connect ETIMEDOUT` issue on a strapi server?

Every time I attempt to execute commands such as start or develop, I encounter errors on my personal computer and am unable to launch the strapi server. Despite already running yarn install to download dependencies, the issue persists. I have also experi ...

Experiencing installation issues with npm on Mac M1, seeking assistance

npm WARNING: old lockfile npm WARNING: old lockfile The package-lock.json file was generated with an outdated version of npm, npm WARNING: old lockfile therefore additional metadata needs to be retrieved from the registry. npm WARNING: old lockfile npm W ...

The formatting in vscode does not apply to .tsx files

For a while now, I've relied on the Prettier extension in Visual Studio Code for formatting my code. However, since switching to writing React with Typescript, I now need to configure Prettier to format .tsx files accordingly. ...

What is the reason for the component property being valued within the subscribe body when it is not defined outside of it?

I am currently facing an issue with retrieving data from a SQL server table using http.get within the constructor of an Angular component 5. While I am able to assign the retrieved data to a component property inside the subscribe method, the value becomes ...

I am currently lacking the "./bin/www" file

I'm completely new to the world of web development, so please bear with me if I don't make much sense. I've been trying to follow a guide on creating a simple restful app, but I encountered an issue when I attempted to use 'npm start&ap ...

The upload function does not allow re-uploading of a file that has been previously deleted

Attempting to upload a file using the following code onDrag(event:any) { console.log(this.toUpload); if(this.toUpload.length >0){ this.error = "Only one file at a time is accepted"; }else{ let fileName = event[0].name; let split ...

I'm experiencing difficulties in establishing a connection from Ionic to my remote database

I set up a database on Fauxten and now I'm trying to connect it to my project. Although I can open the link in my browser, nothing happens when I try to call it in the app. I can't figure out what I'm missing. import { Injectable } from &ap ...

Unresolved (waiting for response) unidentified

I encountered a perplexing error in Chrome and I am unable to identify its source: https://i.sstatic.net/f9Blt.png The only clue I have is that after refactoring approximately 10,000 lines of code, this error surfaced. It occurred during the middle of the ...