【问题标题】:Webpack error - can't resolve loader in 'tmp/build...'Webpack 错误 - 无法解析 'tmp/build...' 中的加载程序
【发布时间】:2017-08-21 04:00:29
【问题描述】:

我最近添加了 eslint 和 eslint-loader NPM 模块作为我的 React 应用程序(使用 Webpack)的开发依赖项 - 当我在本地运行我的开发服务器时一切都很好。但是,当我尝试在本地构建生产版本时,它会失败并出现以下错误:

npm ERR! Darwin 16.7.0
npm ERR! argv "/Users/user/.nvm/versions/node/v6.11.1/bin/node" "/Users/user/.nvm/versions/node/v6.11.1/bin/npm" "run" "build:client"
npm ERR! node v6.11.1
npm ERR! npm  v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! App@0.0.1 build:client: `webpack`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the App@0.0.1 build:client script 'webpack'.

一时兴起,我也尝试推送到 Heroku,但我得到了一个更有帮助的错误:

ERROR in Entry module not found: Error: Can't resolve 'eslint-loader' in '/tmp/build_5f502b6d28fee058cbe484b873b6e7cb'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! App@0.0.1 build:client: `webpack`
npm ERR! Exit status 2

所以这是我承认我对 Webpack 有点困惑的部分,如果可以提供一些指导和澄清,我将不胜感激。我见过其他人有类似的问题,但从未提到过'/tmp/build...' 目录。

首先,这是我的package.json 中的脚本和其他一些相关内容:

"scripts": {
    "start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
    "start:prod": "node server.bundle.js",
    "start:dev": "concurrently --prefix \"[{name}]\" -k \"webpack -d --watch --colors\"  \"nodemon index.js\" -n \"WEBPACK,NODEMON\" --content-base public/",
    "build:client": "webpack",
    "build:server": "webpack --config webpack.server.config.js",
    "build": "NODE_ENV=production npm run build:client && npm run build:server",
    "postinstall": "npm run build"
},
"dependencies": {
    "webpack": "^2.6.0"
},
"devDependencies": {
    "eslint": "^4.4.1",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-babel": "^4.1.2",
    "eslint-plugin-react": "^7.1.0"
    "webpack-dev-server": "^2.4.5"
}

正如错误所说,build:client 是失败的地方...

来自webpack.config.js:

"module": {
    "loaders": [
        {
            "exclude": /node_modules/,
            "loader": "babel-loader",
            "query": { "presets": ["react", "es2015", "stage-2"] },
            "test": /\.jsx?$/
        },
        {
            "exclude": /node_modules/,
            "loader": "babel-loader",
            "test": /\.js$/
        },
        {
            "enforce": "pre",
            "exclude": /node_modules/,
            "loader": "eslint-loader",
            "query": { "presets": ["react", "es2015", "stage-2"] },
            "test": /\.jsx$/
        },
        {
            "enforce": "pre",
            "exclude": /node_modules/,
            "loader": "eslint-loader",
            "test": /\.js$/
        }
    ]
}

如果我在配置中注释掉引用eslint-loader 的部分,那么它构建得很好,所以正如 Heroku 如此敏锐地观察到的那样,问题一定出在这些行上。任何人都可以建议解决此问题吗?我不确定我是否真的了解出了什么问题,更不用说如何解决了。

编辑这是我完整的webpack.config.js:

const webpack = require("webpack")

module.exports = {
    "entry": "./src/index.jsx",
    "module": {
        "loaders": [
            {
                "exclude": /node_modules/,
                "loader": "babel-loader",
                "query": { "presets": ["react", "es2015", "stage-2"] },
                "test": /\.jsx?$/
            },
            {
                "exclude": /node_modules/,
                "loader": "babel-loader",
                "test": /\.js$/
            },
            {
                "enforce": "pre",
                "exclude": /node_modules/,
                "loader": "eslint-loader",
                "query": { "presets": ["react", "es2015", "stage-2"] },
                "test": /\.jsx$/
            },
            {
                "enforce": "pre",
                "exclude": /node_modules/,
                "loader": "eslint-loader",
                "test": /\.js$/
            }
        ]
    },
    "output": {
        "filename": "./public/bundle.js",
        "publicPath": "/"
    },
    "plugins": [
        new webpack.DefinePlugin({
            "API_URL": JSON.stringify(process.env.API_URL || "http://localhost:4000/api/v1"),
            "IS_STAGING": JSON.stringify(process.env.IS_STAGING || "false"),
            "NODE_ENV": JSON.stringify(process.env.NODE_ENV || "development")
        })
    ]
}

【问题讨论】:

  • 我们需要您的 webpack.server.config 哎呀。对不起。我没有看到你的一些信息。不挂断。试试:在你的package.json 添加"build:client": "webpack --config webpack.config.js
  • @archae0pteryx 我试过了,但仍然遇到同样的错误......
  • tmp 文件夹是否存在?如果不做一个。我可能是由于权限而在创建文件夹时遇到问题?只是把它扔在那里
  • @archae0pteryx 好建议,但还是一样的麻烦。
  • 请您发布您的整个 webpack 配置吗?

标签: node.js reactjs webpack


【解决方案1】:

感谢 @Marek 和 @archae0pteryx 对此的想法 - 我想出了一个修复方法,尽管它可能不是最佳的...

基本上,我创建了一个 webpack.dev.config.js 文件,其中包含对 eslint-loader... 的引用,这很好,因为我只想在本地开发中使用 linting。然后我从我的主配置文件中删除了这些引用,更新了package.json 以在本地开发中使用 dev 配置,一切都很好。

【讨论】:

  • f'n'a mang. +1 抱歉,我们无法找出“正确”的解决方案……但是,嘿。有时你只需要破解!很高兴你明白了。
  • @archae0pteryx 非常感谢!
【解决方案2】:

我有同样的问题,但对我来说,这并没有以这种方式解决。

我使用 EvHaus 的这个答案解决了它 https://github.com/babel/babel-loader/issues/166#issuecomment-184763126

这是解决方案:

plugins: [
    require.resolve("babel-plugin-add-module-exports"),
    require.resolve("babel-plugin-transform-decorators-legacy")
],
presets: [
    require.resolve("babel-preset-es2015"),
    require.resolve("babel-preset-stage-0"),
    require.resolve("babel-preset-react")
]

【讨论】:

    【解决方案3】:

    我认为您的问题可能是您在 devDependencies 中有 EsLint。如果我没记错,Heroku 只会安装您的应用程序的 dependencies。尝试将 EsLint 移至 dependencies。

    【讨论】:

    • 感谢 Marek,这是个好主意……但它没有帮助,在本地和 Heroku 部署上仍然存在相同的问题:(
    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多