i have a problem with import statements in my website project. try to use nodejs - typescript and jquery
my project folder looks like that:
- Project
- node_modules
- public
- js
- jquery/dist (copy form node_modules/jquery/dist)
- index.html
- ts
- test.ts
- package.json
- tsconfig.json
package.json:
{
"name": "testclient",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/jquery": "^3.3.4",
"jquery": "^3.3.1"
},
"devDependencies": {
"typescript": "^2.9.2"
}
}
tsconfig.json:
{
"compileOnSave": true,
"compilerOptions": {
"experimentalDecorators": true,
"sourceMap": true,
"strict": true,
"module": "ES2015",
"moduleResolution": "Classic",
"target": "ES5",
"lib": ["dom", "es5", "es2015", "es2015.promise"],
"removeComments": true,
"sourceMap": true,
"outDir": "public/js",
"allowSyntheticDefaultImports": true
},
"include": [
"ts/**/*"
],
"exclude": [
"node_modules"
]
}
index.html
<!DOCTYPE html>
<html lang="de">
<header>
<title>Test</title>
</header>
<body>
Mein Test
</body>
<script type="text/javascript" src="js/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="js/test.js"></script>
</html>
test.ts
import $ from 'jquery';
$(document).ready(() => {
console.log('document is ready now');
console.log($);
});
if i start the index.html in my chrome browser and open the console i get a error:
Uncaught SyntaxError: Unexpected identifier test.js:1
i figured out $ is not known at the moment i call it as jquery. so i cant use jquery in my script
what is the best practice to get it run without using ///<reference path=""/>
for importing jquery. which tsconfig params are to set to get it running in browser?
import $ from 'jquery
to/// <reference path="../node_modules/@types/jquery/index.d.ts"
all works. but thats not the way i search. import statements have to work i think – mtizziani