[英]Error importing empty class, error TS2307: Cannot find module 'menu'
我只是想導入一個簡單的空白導出類。 我不確定為什么找不到文件,因為它和導入文件的類在同一目錄中。 我已經在Google上搜索了類似的錯誤代碼,但沒有任何解決方案對我有用,它是一個相對簡單的問題,所以我很困惑。
錯誤:
error TS2307: Cannot find module 'menu'
資料夾結構:
node_modules/
src/
entry.tsx
menu.tsx
index.html
package-lock.json
package.json
tsconfig.json
webpack.config.js
entry.tsx
import menu from 'menu';
menu.tsx
export default class menu { }
webpack.config
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/js/entry.tsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [ 'css-loader' ]
})
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/
}
]
},
plugins: [
new ExtractTextPlugin({
filename: "bundle.css"
}),
new HtmlWebpackPlugin({
title: 'Custom template',
template: 'index.html'
})
]
};
的package.json
{
"name": "helloworld",
"version": "1.0.0",
"description": "Hello there",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack-dev-server",
"build:prod": "webpack -p"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.7",
"extract-text-webpack-plugin": "^3.0.1",
"html-webpack-plugin": "^2.30.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"style-loader": "^0.19.0",
"ts-loader": "^2.3.7",
"typescript": "^2.5.3",
"webpack": "^3.7.1",
"webpack-dev-server": "^2.9.1"
},
"dependencies": {
"@types/react": "^16.0.13",
"react": "^16.0.0",
"react-dom": "^16.0.0"
}
}
我認為,如果您未在“ include”行中包含相對路徑,則Webpack會嘗試加載Node包。
嘗試在entry.tsx中更改此行:
import menu from 'menu';
對此:
import menu from './menu';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.