5

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?

1
  • Nice question etiquette. Very to the point. Loved reading / answering it Commented Oct 27, 2015 at 4:42

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried, but it doesn't work. webpack can directly load npm component if I am correct. BTW, I love your youtube video. :)
Thanks, It is working now. Still I want to know that, is webpack reading node_modules automatically?
you can always create an alias in your webpack config e.g. ' resolve: { alias: { myNpmModule: path.join(__dirname, '/node_module/myNpmModule/dist/myNpmModule.min')'
is webpack reading node_modules automatically for require calls yes.

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.