using jquery with typescript












1














I am new to this so bear with me. What i am trying to do is getting jquery to work in a typescript code file.



How should i refer to the .d.ts code?



I have tried the following:



import * as $ from "jquery";


this gives me the error: "'jquery' resolves to non-module entity and cannot be imported with this construct"



then i tried:



import $ = require("jquery");


this gives the following error:
Import assignment cannot be used when targeting EcmaScript 2015 modules



How do I go about this to make jquery functions work in my typescript code? Are there other ways?



I am using typescript 2.5.2, module system ES2015, EcmaScript 6 and @types/jquery.





EDIT



i tried import * as $ from "../wwwroot/lib/jQuery/dist/jquery.min.js";, now it gives me the error "... has been resolved ... but --allowJs is not set",I dont have a tsconfig.json yet, I probably need it for the --allowJs command, is that right?










share|improve this question
























  • Try to use relative path to jquery: import * as $ from "path/to/jquery";
    – Commercial Suicide
    Sep 17 '17 at 14:07












  • I am doing import * as jQuery from 'jquery'. It should work if jQuery is installed through NPM.
    – ideaboxer
    Sep 17 '17 at 14:07










  • (at least with TypeScript 2.3.4 it works)
    – ideaboxer
    Sep 17 '17 at 14:09










  • i have installed it through npm as well, i am gonna try to be more specific with my path
    – Stanislaw T
    Sep 17 '17 at 14:09










  • i tried import * as $ from "../node_modules/@types/jquery"; and import * as $ from "../node_modules/@types/jquery/index.d.ts"; and it doesnt work either
    – Stanislaw T
    Sep 17 '17 at 14:14
















1














I am new to this so bear with me. What i am trying to do is getting jquery to work in a typescript code file.



How should i refer to the .d.ts code?



I have tried the following:



import * as $ from "jquery";


this gives me the error: "'jquery' resolves to non-module entity and cannot be imported with this construct"



then i tried:



import $ = require("jquery");


this gives the following error:
Import assignment cannot be used when targeting EcmaScript 2015 modules



How do I go about this to make jquery functions work in my typescript code? Are there other ways?



I am using typescript 2.5.2, module system ES2015, EcmaScript 6 and @types/jquery.





EDIT



i tried import * as $ from "../wwwroot/lib/jQuery/dist/jquery.min.js";, now it gives me the error "... has been resolved ... but --allowJs is not set",I dont have a tsconfig.json yet, I probably need it for the --allowJs command, is that right?










share|improve this question
























  • Try to use relative path to jquery: import * as $ from "path/to/jquery";
    – Commercial Suicide
    Sep 17 '17 at 14:07












  • I am doing import * as jQuery from 'jquery'. It should work if jQuery is installed through NPM.
    – ideaboxer
    Sep 17 '17 at 14:07










  • (at least with TypeScript 2.3.4 it works)
    – ideaboxer
    Sep 17 '17 at 14:09










  • i have installed it through npm as well, i am gonna try to be more specific with my path
    – Stanislaw T
    Sep 17 '17 at 14:09










  • i tried import * as $ from "../node_modules/@types/jquery"; and import * as $ from "../node_modules/@types/jquery/index.d.ts"; and it doesnt work either
    – Stanislaw T
    Sep 17 '17 at 14:14














1












1








1







I am new to this so bear with me. What i am trying to do is getting jquery to work in a typescript code file.



How should i refer to the .d.ts code?



I have tried the following:



import * as $ from "jquery";


this gives me the error: "'jquery' resolves to non-module entity and cannot be imported with this construct"



then i tried:



import $ = require("jquery");


this gives the following error:
Import assignment cannot be used when targeting EcmaScript 2015 modules



How do I go about this to make jquery functions work in my typescript code? Are there other ways?



I am using typescript 2.5.2, module system ES2015, EcmaScript 6 and @types/jquery.





EDIT



i tried import * as $ from "../wwwroot/lib/jQuery/dist/jquery.min.js";, now it gives me the error "... has been resolved ... but --allowJs is not set",I dont have a tsconfig.json yet, I probably need it for the --allowJs command, is that right?










share|improve this question















I am new to this so bear with me. What i am trying to do is getting jquery to work in a typescript code file.



How should i refer to the .d.ts code?



I have tried the following:



import * as $ from "jquery";


this gives me the error: "'jquery' resolves to non-module entity and cannot be imported with this construct"



then i tried:



import $ = require("jquery");


this gives the following error:
Import assignment cannot be used when targeting EcmaScript 2015 modules



How do I go about this to make jquery functions work in my typescript code? Are there other ways?



I am using typescript 2.5.2, module system ES2015, EcmaScript 6 and @types/jquery.





EDIT



i tried import * as $ from "../wwwroot/lib/jQuery/dist/jquery.min.js";, now it gives me the error "... has been resolved ... but --allowJs is not set",I dont have a tsconfig.json yet, I probably need it for the --allowJs command, is that right?







jquery typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 17 '17 at 14:31

























asked Sep 17 '17 at 14:01









Stanislaw T

131212




131212












  • Try to use relative path to jquery: import * as $ from "path/to/jquery";
    – Commercial Suicide
    Sep 17 '17 at 14:07












  • I am doing import * as jQuery from 'jquery'. It should work if jQuery is installed through NPM.
    – ideaboxer
    Sep 17 '17 at 14:07










  • (at least with TypeScript 2.3.4 it works)
    – ideaboxer
    Sep 17 '17 at 14:09










  • i have installed it through npm as well, i am gonna try to be more specific with my path
    – Stanislaw T
    Sep 17 '17 at 14:09










  • i tried import * as $ from "../node_modules/@types/jquery"; and import * as $ from "../node_modules/@types/jquery/index.d.ts"; and it doesnt work either
    – Stanislaw T
    Sep 17 '17 at 14:14


















  • Try to use relative path to jquery: import * as $ from "path/to/jquery";
    – Commercial Suicide
    Sep 17 '17 at 14:07












  • I am doing import * as jQuery from 'jquery'. It should work if jQuery is installed through NPM.
    – ideaboxer
    Sep 17 '17 at 14:07










  • (at least with TypeScript 2.3.4 it works)
    – ideaboxer
    Sep 17 '17 at 14:09










  • i have installed it through npm as well, i am gonna try to be more specific with my path
    – Stanislaw T
    Sep 17 '17 at 14:09










  • i tried import * as $ from "../node_modules/@types/jquery"; and import * as $ from "../node_modules/@types/jquery/index.d.ts"; and it doesnt work either
    – Stanislaw T
    Sep 17 '17 at 14:14
















Try to use relative path to jquery: import * as $ from "path/to/jquery";
– Commercial Suicide
Sep 17 '17 at 14:07






Try to use relative path to jquery: import * as $ from "path/to/jquery";
– Commercial Suicide
Sep 17 '17 at 14:07














I am doing import * as jQuery from 'jquery'. It should work if jQuery is installed through NPM.
– ideaboxer
Sep 17 '17 at 14:07




I am doing import * as jQuery from 'jquery'. It should work if jQuery is installed through NPM.
– ideaboxer
Sep 17 '17 at 14:07












(at least with TypeScript 2.3.4 it works)
– ideaboxer
Sep 17 '17 at 14:09




(at least with TypeScript 2.3.4 it works)
– ideaboxer
Sep 17 '17 at 14:09












i have installed it through npm as well, i am gonna try to be more specific with my path
– Stanislaw T
Sep 17 '17 at 14:09




i have installed it through npm as well, i am gonna try to be more specific with my path
– Stanislaw T
Sep 17 '17 at 14:09












i tried import * as $ from "../node_modules/@types/jquery"; and import * as $ from "../node_modules/@types/jquery/index.d.ts"; and it doesnt work either
– Stanislaw T
Sep 17 '17 at 14:14




i tried import * as $ from "../node_modules/@types/jquery"; and import * as $ from "../node_modules/@types/jquery/index.d.ts"; and it doesnt work either
– Stanislaw T
Sep 17 '17 at 14:14












3 Answers
3






active

oldest

votes


















2














add a reference to the Typescript definition file the top of the file.



/// <reference path ="jquery.d.ts"/> 


instead of



import * as $ from "jquery";


According to the DefinitelyTyped wiki:




A TypeScript declaration file is way of defining the types, functions
and parameters in an external third-party JavaScript library. By using
a declaration file in your TypeScript code will enable Intellisense
and type checking against the external library you are using.




jquery.d.ts is a part of the DefinitelyTyped Library found on GitHub.
and also you can install via npm.






share|improve this answer





















  • this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
    – Stanislaw T
    Sep 21 '17 at 19:45










  • You have to add jquery.js file to page for executing jquery functions.
    – Hamed Javaheri
    Sep 22 '17 at 6:08



















0














I dont know it this is a "correct way" of doing things, but i use the following line in my typescript project to use jquery.



const jquery = require('jquery');
// and then later in your class
jquery('.someclass').show();





share|improve this answer





















  • it gives me the error "Cannot find name 'require' "
    – Stanislaw T
    Sep 17 '17 at 14:43










  • If you want to use require, you need to declare it: declare function require(moduleName: string): any;
    – ideaboxer
    Sep 17 '17 at 15:01



















-1














Installation 1 Terminal



npm install --save jquery


Installation 1 Terminal



npm install --save-dev @types/jquery


installation 3 Add refenece of jquery file in "scripts" array inside angular-cli.json file.



"scripts": [
"../node_modules/jquery/dist/jquery.js"


]



Final - Import



import * as $ from 'jquery';
@Component({
...
})
export class YourComponent{
///...usage
$('.classname').show();
}





share|improve this answer























  • can i know why i got a down vote?? cos i use this in a project currently
    – Uche Dim
    Nov 4 '18 at 20:54













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46264908%2fusing-jquery-with-typescript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














add a reference to the Typescript definition file the top of the file.



/// <reference path ="jquery.d.ts"/> 


instead of



import * as $ from "jquery";


According to the DefinitelyTyped wiki:




A TypeScript declaration file is way of defining the types, functions
and parameters in an external third-party JavaScript library. By using
a declaration file in your TypeScript code will enable Intellisense
and type checking against the external library you are using.




jquery.d.ts is a part of the DefinitelyTyped Library found on GitHub.
and also you can install via npm.






share|improve this answer





















  • this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
    – Stanislaw T
    Sep 21 '17 at 19:45










  • You have to add jquery.js file to page for executing jquery functions.
    – Hamed Javaheri
    Sep 22 '17 at 6:08
















2














add a reference to the Typescript definition file the top of the file.



/// <reference path ="jquery.d.ts"/> 


instead of



import * as $ from "jquery";


According to the DefinitelyTyped wiki:




A TypeScript declaration file is way of defining the types, functions
and parameters in an external third-party JavaScript library. By using
a declaration file in your TypeScript code will enable Intellisense
and type checking against the external library you are using.




jquery.d.ts is a part of the DefinitelyTyped Library found on GitHub.
and also you can install via npm.






share|improve this answer





















  • this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
    – Stanislaw T
    Sep 21 '17 at 19:45










  • You have to add jquery.js file to page for executing jquery functions.
    – Hamed Javaheri
    Sep 22 '17 at 6:08














2












2








2






add a reference to the Typescript definition file the top of the file.



/// <reference path ="jquery.d.ts"/> 


instead of



import * as $ from "jquery";


According to the DefinitelyTyped wiki:




A TypeScript declaration file is way of defining the types, functions
and parameters in an external third-party JavaScript library. By using
a declaration file in your TypeScript code will enable Intellisense
and type checking against the external library you are using.




jquery.d.ts is a part of the DefinitelyTyped Library found on GitHub.
and also you can install via npm.






share|improve this answer












add a reference to the Typescript definition file the top of the file.



/// <reference path ="jquery.d.ts"/> 


instead of



import * as $ from "jquery";


According to the DefinitelyTyped wiki:




A TypeScript declaration file is way of defining the types, functions
and parameters in an external third-party JavaScript library. By using
a declaration file in your TypeScript code will enable Intellisense
and type checking against the external library you are using.




jquery.d.ts is a part of the DefinitelyTyped Library found on GitHub.
and also you can install via npm.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 17 '17 at 15:49









Hamed Javaheri

400310




400310












  • this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
    – Stanislaw T
    Sep 21 '17 at 19:45










  • You have to add jquery.js file to page for executing jquery functions.
    – Hamed Javaheri
    Sep 22 '17 at 6:08


















  • this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
    – Stanislaw T
    Sep 21 '17 at 19:45










  • You have to add jquery.js file to page for executing jquery functions.
    – Hamed Javaheri
    Sep 22 '17 at 6:08
















this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
– Stanislaw T
Sep 21 '17 at 19:45




this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
– Stanislaw T
Sep 21 '17 at 19:45












You have to add jquery.js file to page for executing jquery functions.
– Hamed Javaheri
Sep 22 '17 at 6:08




You have to add jquery.js file to page for executing jquery functions.
– Hamed Javaheri
Sep 22 '17 at 6:08













0














I dont know it this is a "correct way" of doing things, but i use the following line in my typescript project to use jquery.



const jquery = require('jquery');
// and then later in your class
jquery('.someclass').show();





share|improve this answer





















  • it gives me the error "Cannot find name 'require' "
    – Stanislaw T
    Sep 17 '17 at 14:43










  • If you want to use require, you need to declare it: declare function require(moduleName: string): any;
    – ideaboxer
    Sep 17 '17 at 15:01
















0














I dont know it this is a "correct way" of doing things, but i use the following line in my typescript project to use jquery.



const jquery = require('jquery');
// and then later in your class
jquery('.someclass').show();





share|improve this answer





















  • it gives me the error "Cannot find name 'require' "
    – Stanislaw T
    Sep 17 '17 at 14:43










  • If you want to use require, you need to declare it: declare function require(moduleName: string): any;
    – ideaboxer
    Sep 17 '17 at 15:01














0












0








0






I dont know it this is a "correct way" of doing things, but i use the following line in my typescript project to use jquery.



const jquery = require('jquery');
// and then later in your class
jquery('.someclass').show();





share|improve this answer












I dont know it this is a "correct way" of doing things, but i use the following line in my typescript project to use jquery.



const jquery = require('jquery');
// and then later in your class
jquery('.someclass').show();






share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 17 '17 at 14:35









Rainer Plumer

2,36311124




2,36311124












  • it gives me the error "Cannot find name 'require' "
    – Stanislaw T
    Sep 17 '17 at 14:43










  • If you want to use require, you need to declare it: declare function require(moduleName: string): any;
    – ideaboxer
    Sep 17 '17 at 15:01


















  • it gives me the error "Cannot find name 'require' "
    – Stanislaw T
    Sep 17 '17 at 14:43










  • If you want to use require, you need to declare it: declare function require(moduleName: string): any;
    – ideaboxer
    Sep 17 '17 at 15:01
















it gives me the error "Cannot find name 'require' "
– Stanislaw T
Sep 17 '17 at 14:43




it gives me the error "Cannot find name 'require' "
– Stanislaw T
Sep 17 '17 at 14:43












If you want to use require, you need to declare it: declare function require(moduleName: string): any;
– ideaboxer
Sep 17 '17 at 15:01




If you want to use require, you need to declare it: declare function require(moduleName: string): any;
– ideaboxer
Sep 17 '17 at 15:01











-1














Installation 1 Terminal



npm install --save jquery


Installation 1 Terminal



npm install --save-dev @types/jquery


installation 3 Add refenece of jquery file in "scripts" array inside angular-cli.json file.



"scripts": [
"../node_modules/jquery/dist/jquery.js"


]



Final - Import



import * as $ from 'jquery';
@Component({
...
})
export class YourComponent{
///...usage
$('.classname').show();
}





share|improve this answer























  • can i know why i got a down vote?? cos i use this in a project currently
    – Uche Dim
    Nov 4 '18 at 20:54


















-1














Installation 1 Terminal



npm install --save jquery


Installation 1 Terminal



npm install --save-dev @types/jquery


installation 3 Add refenece of jquery file in "scripts" array inside angular-cli.json file.



"scripts": [
"../node_modules/jquery/dist/jquery.js"


]



Final - Import



import * as $ from 'jquery';
@Component({
...
})
export class YourComponent{
///...usage
$('.classname').show();
}





share|improve this answer























  • can i know why i got a down vote?? cos i use this in a project currently
    – Uche Dim
    Nov 4 '18 at 20:54
















-1












-1








-1






Installation 1 Terminal



npm install --save jquery


Installation 1 Terminal



npm install --save-dev @types/jquery


installation 3 Add refenece of jquery file in "scripts" array inside angular-cli.json file.



"scripts": [
"../node_modules/jquery/dist/jquery.js"


]



Final - Import



import * as $ from 'jquery';
@Component({
...
})
export class YourComponent{
///...usage
$('.classname').show();
}





share|improve this answer














Installation 1 Terminal



npm install --save jquery


Installation 1 Terminal



npm install --save-dev @types/jquery


installation 3 Add refenece of jquery file in "scripts" array inside angular-cli.json file.



"scripts": [
"../node_modules/jquery/dist/jquery.js"


]



Final - Import



import * as $ from 'jquery';
@Component({
...
})
export class YourComponent{
///...usage
$('.classname').show();
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 10:18

























answered Dec 16 '17 at 2:24









Uche Dim

21036




21036












  • can i know why i got a down vote?? cos i use this in a project currently
    – Uche Dim
    Nov 4 '18 at 20:54




















  • can i know why i got a down vote?? cos i use this in a project currently
    – Uche Dim
    Nov 4 '18 at 20:54


















can i know why i got a down vote?? cos i use this in a project currently
– Uche Dim
Nov 4 '18 at 20:54






can i know why i got a down vote?? cos i use this in a project currently
– Uche Dim
Nov 4 '18 at 20:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46264908%2fusing-jquery-with-typescript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)