简体   繁体   English

运行“ npm run”时无法解析“反应”

[英]Can't resolve 'react' when running “npm run”

My project has recently started complaining that it can't resolve react when I run the npm run dev script, which is: cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js 我的项目最近开始抱怨说,当我运行npm run dev脚本时,它无法解决反应,即:cross-env NODE_ENV = development node_modules / webpack / bin / webpack.js --progress --hide-modules- config = node_modules / laravel-mix / setup / webpack.config.js

It seems like npm can't find the node_modules directory? 看来npm找不到node_modules目录?

It runs fine on my windows machine, but when I upload it to the Ubuntu server I get errors. 它可以在Windows机器上正常运行,但是当我将其上传到Ubuntu服务器时,会出现错误。 I've tried various things, like deleting node_modules and the lock file, cleaning the npm cache, checked $NODE_PATH, reinstalled nodejs and npm to no avail. 我尝试了各种操作,例如删除node_modules和锁定文件,清理npm缓存,检查$ NODE_PATH,重新安装nodejs和npm无济于事。 I can't figure out if this is a path issue or I'm missing something on the server or what. 我不知道这是路径问题还是服务器上缺少什么或什么。

I'd appreciate any tips you have. 如果您有任何提示,我将不胜感激。

My package.json: 我的package.json:

{
    "private": true,
    "name": "Write.nogetdigitalt.dk",
    "version": "0.0.1",
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "@fortawesome/react-fontawesome": "^0.1.3",
        "axios": "^0.18",
        "babel-preset-react": "^6.24.1",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^2.0",
        "lodash": "^4.17.5",
        "moment": "^2.23.0",
        "popper.js": "^1.12",
        "react": "^16.5.2",
        "react-datepicker": "^2.0.0",
        "react-dom": "^16.5.2",
        "redux": "^4.0.1",
        "vue": "^2.5.7"
    },
    "dependencies": {
        "@fortawesome/free-solid-svg-icons": "^5.5.0",
        "react-icons": "^3.2.0"
    }
}

webpack.mix.js webpack.mix.js

const mix = require('laravel-mix');
const webpack = require('webpack');

mix.webpackConfig({
    resolve: {
        extensions: ['.js', 'jsx'],
        alias: {
            'react': 'React'
        }
    },
    plugins: [
        new webpack.ProvidePlugin({
            'React': 'react',
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            Popper: ['popper.js', 'default']
        })
    ]
})
.disableNotifications()

.react('resources/js/app.js', 'public/js')
    .sourceMaps()
.sass('resources/sass/app.scss', 'public/css');

Error messages: 错误讯息:

ERROR in ./resources/js/pages/Note/Note.js
Module not found: Error: Can't resolve 'react' in '/var/www/html/write.nogetdigitalt.dk/resources/js/pages/Note'
resolve 'react' in '/var/www/html/write.nogetdigitalt.dk/resources/js/pages/Note'
  Parsed request is a module
  using description file: /var/www/html/write.nogetdigitalt.dk/package.json (relative path: ./resources/js/pages/Note)
    aliased with mapping 'react': 'React' to 'React'
      Parsed request is a module
      using description file: /var/www/html/write.nogetdigitalt.dk/package.json (relative path: ./resources/js/pages/Note)
        Field 'browser' doesn't contain a valid alias configuration
      after using description file: /var/www/html/write.nogetdigitalt.dk/package.json (relative path: ./resources/js/pages/Note)
        resolve as module
          /var/www/html/write.nogetdigitalt.dk/resources/js/pages/Note/node_modules doesn't exist or is not a directory
          /var/www/html/write.nogetdigitalt.dk/resources/js/pages/node_modules doesn't exist or is not a directory
          /var/www/html/write.nogetdigitalt.dk/resources/js/node_modules doesn't exist or is not a directory
          /var/www/html/write.nogetdigitalt.dk/resources/node_modules doesn't exist or is not a directory
          /var/www/html/node_modules doesn't exist or is not a directory
          /var/www/node_modules doesn't exist or is not a directory
          /var/node_modules doesn't exist or is not a directory
          /node_modules doesn't exist or is not a directory

You don't have installed package dependencies. 您尚未安装软件包依赖项。

npm install

After execute that node-module directorie has created. 执行后,该节点模块目录已创建。 And launch your application. 并启动您的应用程序。

npm run developement 

For anyone else getting this kind of problems, for path definition between different OS is better to use path from NodeJS 对于其他遇到这种问题的人,不同操作系统之间的路径定义最好使用NodeJS中的path

`const path = require('path');
[...]
resolve: {
        extensions: ['.js', 'jsx'],
        alias: {
            'react': path.resolve(__dirname, 'Path to your folder to alias')
        }
    }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM