webpack无法加载模块jquery

时间:2015-10-27 04:40:28

标签: jquery node.js typescript webpack

我正在尝试使用webpack在typescript中导入jquery。这就是我所做的。

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

在文件中,我写了这个

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

然后创建app.ts并编写以下内容

import $ = require('jquery');

然后我npm install jquery --save加载jquery组件。

然后当我执行webpack时,它无法找到模块'jquery'消息。

ts-loader: Using typescript@1.6.2
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'.

有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:3)

  

有人可以告诉我我做错了吗?

您需要jquery定义:

tsd install jquery --save

你还需要一个tsconfig.json:

tsc -init会为您生成一个。但我建议:

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

您还需要安装typescript

npm install typescript -save

相关问题