gpt4 book ai didi

reactjs - 导入 .jsx 文件时找不到模块

转载 作者:行者123 更新时间:2023-12-03 13:36:39 25 4
gpt4 key购买 nike

我找不到解决方案。我正在使用 Reactstrap(CSS 框架)、React、Express 和 Webpack。

我成功在index.jsx上导入App.jsx文件。然后,我尝试使用相同的方式在 App.jsx 上导入 NavbarTemplate.jsx 文件。但是,它显示这样的错误:

ERROR in ./client/src/components/App.jsx Module not found: Error: Can't resolve 'NavbarTemplate.jsx' in '/Users/oprent1/v2/oprent-react/client/src/components' @ ./client/src/components/App.jsx 11:22-51 @ ./client/src/index.jsx

我的配置有什么问题吗?我在下面提供了几个与此相关的文件:

webpack.config.js

const path = require('path');

module.exports = {
entry: path.join(__dirname, '/client/src/index.jsx'),
output: {
path: path.join(__dirname, '/client/dist/js'),
filename: 'app.js',
},

module: {
loaders: [
{
test: /\.jsx?$/,
include: path.join(__dirname, '/client/src'),
// loader: 'babel',
loader: 'babel-loader',
query: {
presets: ["react", "es2015"],
plugins: ["transform-class-properties"]
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
],
},
watch: true
};

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import Bootstrap from 'bootstrap/dist/css/bootstrap.css';
import App from './components/App.jsx'; //SUCCESS when imported

ReactDOM.render(
<App />,
document.getElementById('react-app')
);

App.jsx

import React from 'react';
import NavbarTemplate from 'NavbarTemplate.jsx'; //ERROR when imported

const App = (props) => {
return (
<div>
<NavbarTemplate />
</div>
);
};

export default App;

NavbarTemplate.jsx

import React, { PropTypes } from 'react';
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavItem, NavLink } from 'reactstrap';

class NavbarTemplate extends React.Component {

constructor(props) {
super(props);

this.toggleNavbar = this.toggleNavbar.bind(this);
this.state = {
collapsed: true
};
}

toggleNavbar() {
this.setState({
collapsed: !this.state.collapsed
});
}

render() {
return (
<div>
<Navbar color="faded" light>
<NavbarToggler onClick={this.toggleNavbar} />
<Collapse className="navbar-toggleable-md" isOpen={!this.state.collapsed}>
<NavbarBrand href="/">reactstrap</NavbarBrand>
<Nav navbar>
<NavItem>
<NavLink href="/components/">Components</NavLink>
</NavItem>
<NavItem>
<NavLink href="https://github.com/reactstrap/reactstrap">Github</NavLink>
</NavItem>
</Nav>
</Collapse>
</Navbar>
</div>
);
}
}

export default NavbarTemplate;

文件夹结构

enter image description here

最佳答案

根据webpack can't find module if file named jsx如果您不想在导入时添加 .jsx ,您可以将 resolve: { extensions: ['.js', '.jsx'] } 添加到您的 webpack.config.

// ...
module: {
loaders: [
{
test: /\.jsx?$/,
include: path.join(__dirname, '/client/src'),
// loader: 'babel',
loader: 'babel-loader',
query: {
presets: ["react", "es2015"],
plugins: ["transform-class-properties"]
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
]
},
resolve: {
extensions: ['.js', '.jsx']
}
// ...

关于reactjs - 导入 .jsx 文件时找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45930746/

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