Reactjs 未找到模块:错误:Can';t解决';openlayers';打字
我正在尝试将openlayers与typescript一起使用,但存在一个问题 openlayers用于MapSection组件,下面是MapSection代码Reactjs 未找到模块:错误:Can';t解决';openlayers';打字,reactjs,typescript,webpack,openlayers,Reactjs,Typescript,Webpack,Openlayers,我正在尝试将openlayers与typescript一起使用,但存在一个问题 openlayers用于MapSection组件,下面是MapSection代码 import React,{useLayoutEffect}来自“React” 从“openlayers”导入*作为ol; 常量映射节:React.FC=()=>{ useLayoutEffect(()=>{ 常量映射=新ol.map({ 图层:[ 新ol.layer.Tile({ source:new ol.source.OSM(),
import React,{useLayoutEffect}来自“React”
从“openlayers”导入*作为ol;
常量映射节:React.FC=()=>{
useLayoutEffect(()=>{
常量映射=新ol.map({
图层:[
新ol.layer.Tile({
source:new ol.source.OSM(),
})],
目标:'地图部分',
视图:新ol.view({
中间:[36,38],
缩放:16,
}),
控件:[],
});
map.getKeys();
}, [])
返回(
)
}
导出默认映射节;
如果我运行webpack devServer,它会给出如下错误消息
//webpack.config.js
import path from "path";
import HTMLWebpackPlugin from 'html-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import webpack from "webpack";
const config: webpack.Configuration = {
entry: path.join(__dirname, 'src', 'index.tsx'),
context: path.resolve(__dirname, '.'),
devtool: 'source-map',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './build'),
publicPath: '/'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', 'json'],
modules: [
'node_modules',
path.resolve(__dirname, './node_modules')
],
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
]
},
plugins: [
new HTMLWebpackPlugin({
template: './public/index.html'
}),
new ForkTsCheckerWebpackPlugin({
async: false,
eslint: {
files: "./src/**/*",
},
}),
]
}
export default config;
未找到./src/components/MapSection.tsx模块中的错误:错误:无法解析“/Users/donghokim/recruit/angelswing-frontend-test-0.3/src/components'../src/components/MapSection.tsx”中的“openlayers”。/src/components/MapSection.tsx 2:0-33 6:18-24 7:19-27 8:20-29 11:16-23@./src/components/App.tsx@./src/index.tsx
奇怪的是,组件试图在当前目录中找到openlayers库
webpack和tsconfig文件是这样编写的
//webpack.config.js
import path from "path";
import HTMLWebpackPlugin from 'html-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import webpack from "webpack";
const config: webpack.Configuration = {
entry: path.join(__dirname, 'src', 'index.tsx'),
context: path.resolve(__dirname, '.'),
devtool: 'source-map',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './build'),
publicPath: '/'
},
resolve: {
extensions: ['.ts', '.tsx', '.js', 'json'],
modules: [
'node_modules',
path.resolve(__dirname, './node_modules')
],
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
]
},
plugins: [
new HTMLWebpackPlugin({
template: './public/index.html'
}),
new ForkTsCheckerWebpackPlugin({
async: false,
eslint: {
files: "./src/**/*",
},
}),
]
}
export default config;
我只是想知道我在哪里可以得到一个开始发现问题的分数
///添加了package.json设置
...
"dependencies": {
"ol": "^6.4.3",
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.12.1",
"@babel/runtime": "^7.12.1",
"@types/fork-ts-checker-webpack-plugin": "^0.4.5",
"@types/openlayers": "^4.6.17",
"@types/react": "^16.9.55",
"@types/react-dom": "^16.9.9",
"@types/styled-components": "^5.1.4",
"@types/webpack": "^4.41.24",
"@types/webpack-dev-server": "^3.11.0",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"babel-loader": "^8.1.0",
"css-loader": "^4.3.0",
"eslint": "^7.12.1",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"fork-ts-checker-webpack-plugin": "^5.2.1",
"html-webpack-plugin": "^4.5.0",
"style-loader": "^1.3.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.5",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
}
你确定你已经安装了
openlayers
package吗?节点包名为ol
onlyah,我会添加package.json dependencies我想这个问题已经存在了这个问题已经解决了!我知道Kanti给出的链接中还有另一个openlayers类型包。如果使用@types/ol包而不是@types/openlayers-webpack和typescript,请根据需要查找包。谢谢大家!!