无法使用打字稿解析'jquery' [英] Can't resolve 'jquery' with typescript
问题描述
我在项目中将打字稿与jquery集成时遇到问题.我使用npm安装@ types/jquery和jquery,但是无法在.ts中使用. 我尝试导入:
I have a problem to integrate typescript with jquery in my project. I install both @types/jquery and jquery using npm, but I can't use in my .ts. I try to import with:
import jquery from "jquery";
import * as $ from "jquery";
import jquery = require("jquery");
const jquery = require("jquery");
import jquery = require("@types/jquery");
这些导入在编译时显示错误:
These imports show error in compile time:
Module not found: Error: Can't resolve 'jquery'
我的webpack配置:
My webpack config:
module.exports = {
entry: './src/App.ts',
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'DQuiz'
})
],
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
},
{ test: /\.hbs$/, loader: "handlebars-loader" }
]
},
resolve: {
mainFields: ['browserify'],
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};
推荐答案
我测试了使用ubuntu在另一台计算机上构建项目.我收到相同的错误:
I tested build my project in another machine with ubuntu. I get same error:
Module not found: Error: Can't resolve 'jquery'
通过一些搜索,我找到了解决方案.对于Jquery 3.x,您需要使用'jquery/src/jquery'导入模块.
With some searchs I found a solution. For Jquery 3.x, you need to import module with 'jquery/src/jquery'.
我将Tan Duong建议的解决方案与provide插件一起使用,我认为该解决方案对于jquery更为优雅,然后导入,您可以使用:
I use the solution suggested by Tan Duong with provide plugin, I think this solution is more elegant for jquery, then to import, you can use:
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery/src/jquery',
jquery: 'jquery/src/jquery'
})
]
}
谢谢大家的回答:)
这篇关于无法使用打字稿解析'jquery'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!