webpack은 jsx라는 파일 인 경우 모듈을 찾을 수 없습니다.
webpack.config.js를 이렇게 작성하면
module.exports = {
entry: './index.jsx',
output: {
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}]
}
};
그리고 모듈을 index.jsx
가져옵니다.react
App
import React from 'react';
import { render } from 'react-dom';
import App from './containers/App';
let rootElement = document.getElementById('box')
render(
<App />,
rootElement
)
에서 module app이라는 이름을 지정하면 App.jsx
webpack에서 in't index.jsx
find module App
이라는 메시지가 표시되지만에서 이름이 module app으로 지정 App.js
되면이 모듈을 찾아서 잘 작동합니다.
그래서 나는 그것에 대해 혼란 스럽습니다. 내 webpack.config.js
에서 test: /\.jsx?$/
파일을 확인하기 위해 작성했지만 왜 named *.jsx
를 찾을 수 없습니까?
Webpack은 .jsx
암시 적으로 파일 을 확인하지 않습니다 . 앱에서 파일 확장자를 지정할 수 있습니다 ( import App from './containers/App.jsx';
). 현재 로더 테스트는 jsx 확장자를 가진 파일을 명시 적으로 가져올 때 babel 로더를 사용하도록 말합니다.
또는 .jsx
명시적인 선언없이 웹팩이 해결해야하는 확장에 포함 할 수 있습니다 .
module.exports = {
entry: './index.jsx',
output: {
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}]
},
resolve: {
extensions: ['', '.js', '.jsx'],
}
};
Webpack 2의 경우 빈 확장을 그대로 둡니다.
resolve: {
extensions: ['.js', '.jsx']
}
위의 답변에 추가하여
해결의 모든 응용 프로그램에서 사용하는 파일 형식을 추가해야 할 경우 속성입니다.
.jsx 또는 .es6 파일 을 사용한다고 가정 합니다. 기꺼이 여기에 포함 할 수 있으며 webpack에서 해결합니다.
resolve: {
extensions: ["", ".js", ".jsx", ".es6"]
}
resolve 속성에 확장을 추가 하면 import 문에서 제거 할 수 있습니다.
import Hello from './hello'; // Extensions removed
import World from './world';
커피 스크립트를 감지하려는 경우와 같은 다른 시나리오에서는 다음과 같이 테스트 속성을 구성해야합니다 .
// webpack.config.js
module.exports = {
entry: './main.js',
output: {
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.coffee$/, loader: 'coffee-loader' },
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
resolve: {
// you can now require('file') instead of require('file.coffee')
extensions: ['', '.js', '.json', '.coffee']
}
};
In the interest of readability and copy-paste coding. Here is the webpack 4 answer from mr rogers comment in this thread.
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
resolve: {
extensions: [".js", ".jsx"]
},
use: {
loader: "babel-loader"
}
},
]
}
As mentioned in the comments on the answer from @max, for webpack 4, I found that I needed to put this in one of the rules that were listed under module, like so:
{
module: {
rules: [
{
test: /\.jsx?$/,
resolve: {
extensions: [".js", ".jsx"]
},
include: ...
}
]
}
}
I was facing similar issue, and was able to resolve using resolve property.
const path = require('path');
module.exports = {
entry: './src/app.jsx',
output: {
path: path.join(__dirname,'public'),
filename: 'bundle.js'
},
module : {
rules: [{
loader: 'babel-loader',
test: /\.jsx$/,
exclude: /node_modules/
}]
},
resolve: {
extensions: ['.js', '.jsx']
}
}
As You can see I have used .jsx in there which resolved following error
ERROR in ./src/app.jsx Module not found: Error: Can't resolve
For Reference: https://webpack.js.org/configuration/resolve/#resolve-extensions
Verify, that bundle.js is being generated without errors (check the Task Runner Log).
구성 요소 렌더링 기능에서 html의 구문 오류로 인해 'jsx라는 파일이 있으면 모듈을 찾을 수 없습니다'가 표시되었습니다.
참고 URL : https://stackoverflow.com/questions/34678314/webpack-cant-find-module-if-file-named-jsx
'development' 카테고리의 다른 글
Java의 평가 순서에 대한 규칙은 무엇입니까? (0) | 2020.09.23 |
---|---|
64 비트 또는 32 비트 노드 실행 파일이 설치되어 있는지 확인하는 방법은 무엇입니까? (0) | 2020.09.23 |
앵커 텍스트를 jquery로 교체 (0) | 2020.09.23 |
GhostDoc을 사용하여 Visual Studio에서 모든 TODO 항목보기 (0) | 2020.09.23 |
Homebrew를 통해 Memcached를 설치했습니다. 서버를 시작하고 중지하는 방법은 무엇입니까? (0) | 2020.09.23 |