【问题标题】:Why do I get a Can not find module error when I run npx run dev?为什么我在运行 npx run dev 时会出现找不到模块错误?
【发布时间】:2019-07-25 16:50:58
【问题描述】:

为什么我在运行 npx run dev 时会出现找不到模块错误?

在运行'npx run dev'之前

我处理了这个

运行 npm init 命令 webpack webpack-cli 安装 配置 webpack.config.js 设置 package.json 节点升级 创建 client.jsx WordRelay.jsx 组件文件创建等

webpack.config.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"
  }
}

和github: https://github.com/hyunsokstar/react_game_prac2

【问题讨论】:

    标签: node.js


    【解决方案1】:

    你运行 webpack,但你不让 webpack 知道它应该采取什么配置。所以它将使用默认配置运行。其中entry 设置为src/index.js。您需要运行 webpack 并将其指向您的配置文件(文件名不正确,请修复它)。

    另一个问题是您在代码中使用了jsx,但没有使用babeljsx 编译为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
    

    Difference between npx and npm?

    【讨论】:

    • 谢谢,出现另一个错误 ERROR in Entry module not found: Error: Can't resolve './src' in 'C:\react_game_prac2' 你能帮我吗?
    • 请分享您的项目结构。我认为问题在于您错误地设置了entry
    • git hub 是这样的:github.com/hyunsokstar/react_game_prac2 谢谢
    • react_game_prac2 是 root 和 github 一样,谢谢
    • 我猜是文件名是 config.js 造成的。我还安装了一个杠铃装载机。问题已解决。非常感谢。
    猜你喜欢
    • 2021-05-23
    • 2021-04-28
    • 2019-03-11
    • 1970-01-01
    • 2020-06-10
    • 2019-12-29
    • 2022-08-15
    • 2021-09-13
    • 2019-12-02
    相关资源
    最近更新 更多