Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack cannot load module jquery

I am trying to import jquery in typescript using webpack. This is what I did.

npm init -y
npm install -g webpack
npm install ts-loader --save
touch webpack.config.js

In the file, I wrote this

module.exports = {  
  entry: './app.ts',
  output: {
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
  },
  module: {
    loaders: [
      { test: /\.ts$/, loader: 'ts-loader' }
    ]
  }
}

Then create the app.ts and write the following

import $ = require('jquery');

then I npm install jquery --save to load the jquery component.

then when I execute webpack it gives me cannot find module 'jquery' message.

ts-loader: Using [email protected]
Hash: af60501e920b87c93349
Version: webpack 1.12.2
Time: 1044ms
    Asset     Size  Chunks             Chunk Names
bundle.js  1.39 kB       0  [emitted]  main
    + 1 hidden modules

ERROR in ./app.ts
(1,20): error TS2307: Cannot find module 'jquery'.

Can somebody tell me what I did wrong?

like image 765
user454232 Avatar asked Oct 19 '22 23:10

user454232


1 Answers

Can somebody tell me what I did wrong?

You need the jquery definitions:

tsd install jquery --save

Also you need to have a tsconfig.json:

tsc -init will generate one for you. But I recommend:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5"
    },
    "exclude": [
        "node_modules"
    ]
}

Also you need to install typescript

npm install typescript -save

like image 165
basarat Avatar answered Oct 22 '22 01:10

basarat