gpt4 book ai didi

javascript - 导入空类时出错,错误 TS2307 : Cannot find module 'menu'

转载 作者:行者123 更新时间:2023-11-28 14:44:41 26 4
gpt4 key购买 nike

我只是想导入一个简单的空白导出类。我不确定为什么它找不到该文件,因为它与导入它的类位于同一目录中。我在谷歌上搜索了类似的错误代码,但没有任何解决方案对我有用,而且这是一个相对简单的问题,所以我很困惑。

错误:

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

条目.tsx

import menu from '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中的这一行:

从“菜单”导入菜单;

对此:

从“./menu”导入菜单;

关于javascript - 导入空类时出错,错误 TS2307 : Cannot find module 'menu' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46818049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com