为什么在运行npx run dev时会出现“找不到模块”错误?
在运行“ npx run dev”之前
我处理了这个
运行npm init命令 Webpack webpack-cli安装 配置webpack.conifig.js 设置package.json 节点升级 创建client.jsx WordRelay.jsx组件文件的创建等。
webpack.conifig.js
const path = require('path');
module.exports = {
name:'wordrelay-setting',
mode:'development',
devtool:'eval',
resolve:{
extensions:['.js','.jsx']
},
entry:{
app:['./client']
}, // 입력
output: {
path:path.join(__dirname,'dist'),
filename:'app.js'
}, // 출력
};
package.json
{
"name": "lecture",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack"
},
"author": "hyun",
"license": "ISC",
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"webpack": "^4.37.0",
"webpack-cli": "^3.3.6"
}
}
答案 0 :(得分:1)
您运行webpack,但不让webpack知道它应该采用什么配置。因此它将以默认配置运行。将entry
设置为src/index.js
的情况。您需要运行webpack并将其指向您的配置文件(文件名不正确,因此请对其进行修复)。
另一个问题是,您在代码中使用了jsx
,但是没有使用babel
将jsx
编译为js
。因此,您需要向项目中添加babel
。
最后一个问题是您在client.jsx
文件中有一些错误语法。
package.json
{
"name": "lecture",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack --config webpack.config.js"
},
"author": "hyun",
"license": "ISC",
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"babel-loader": "^8.0.6",
"babel-preset-react-app": "^9.0.0",
"webpack": "^4.37.0",
"webpack-cli": "^3.3.6"
},
"babel": {
"presets": [
"react-app"
]
}
}
webpack.config.js
const path = require('path');
process.env.NODE_ENV='development';
module.exports = {
name:'wordrelay-setting',
mode:'development',
devtool:'eval',
resolve:{
extensions:['.js','.jsx']
},
entry:{
app:['./client']
}, // 입력
output: {
path:path.join(__dirname,'dist'),
filename:'app.js'
}, // 출력
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
}
client.jsx
const React = require('react');
const ReactDom = require('react-dom');
const WordRelay = require('./WordRelay')
React.render(<WordRelay />, document.querySelector('#root'));
原始答案
使用此命令npx run dev
时。您正在执行run
模块。这就是为什么您得到错误。正确的命令应该是
npm run dev