[英]Module not found: Error: Can't resolve 'openlayers' in typescript
I'm trying to use openlayers with typescript, But there is a problem.我正在尝试将 openlayers 与 typescript 一起使用,但是有一个问题。
openlayers is used in MapSection component, below is the MapSection code openlayers用于MapSection组件,下面是MapSection代码
import React, { useLayoutEffect } from 'react'
import * as ol from 'openlayers';
const MapSection: React.FC = () => {
useLayoutEffect(() => {
const map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
})],
target: 'map-section',
view: new ol.View({
center: [36, 38],
zoom: 16,
}),
controls: [],
});
map.getKeys();
}, [])
return (
<section className='map-section'>
</section>
)
}
export default MapSection;
If I run webpack devServer, it gives error message like this如果我运行 webpack devServer,它会给出这样的错误消息
ERROR in./src/components/MapSection.tsx Module not found: Error: Can't resolve 'openlayers' in '/Users/donghokim/recruit/angelswing-frontend-test-0.3/src/components' @./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
It is weird that component try to find openlayers library in current directory.奇怪的是组件试图在当前目录中找到 openlayers 库。
webpack and tsconfig files are written like this 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;
//tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"outDir": "build",
"module": "CommonJS",
"target": "ES5",
"jsx": "react",
"moduleResolution": "node",
"experimentalDecorators": true,
"skipDefaultLibCheck": true,
"sourceMap": true,
"allowJs": true,
"strict": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"]
},
"include": ["src"],
"exclude": ["build/**/*"]
}
I just want to know where can I get a point to start finding problem.我只想知道从哪里可以找到开始发现问题的点。
/// added package.json setting /// 添加了 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"
}
use npm i --save-dev @types/ol
and then include the dependencies like:使用npm i --save-dev @types/ol
然后包括如下依赖项:
import {Point, MultiLineString, MultiPolygon, Polygon, MultiPoint, LinearRing, LineString} from "ol/geom";
const reader = new jsts.io.WKTReader();
const writer = new jsts.io.WKTWriter();
const parser = new jsts.io.OL3Parser();
parser.inject(
Point,
LineString,
LinearRing,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon
);
Then you can close this question please;)那么你可以关闭这个问题了;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.