jquery - webpack 无法加载模块 jquery

标签 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'.

谁能告诉我我做错了什么?

最佳答案

Can somebody tell me what I did wrong?

您需要 jquery 定义:

tsd install jquery --save

你还需要一个 tsconfig.json:

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

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

还需要安装 typescript

npm install typescript -save

关于jquery - webpack 无法加载模块 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360079/

相关文章:

typescript - 你如何猴子修补明确类型的定义

angular - 滚动到底部; scrollTop = scrollHeight 运行得太早?

php - jQuery 中两列的乘法

php - $_POST 未通过 AJAX 调用设置

node.js - 本地化 Nodejs (i18n)

node.js - Azure 上的 Node 服务器 HTTPS 和 HTTP

javascript - 我的 Flimsy AJAX 文件上传出了什么问题?

jquery - 在 ASP.NET MVC 内容占位符中使用 jQuery

javascript - 如何在 NEXT JS 中的路由之间传递状态?

typescript - 使用异步迭代在 TypeScript 中定位 ES5 并在浏览器上运行时出错