首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack错误-无法解决加载程序在‘tmp/构建.’

Webpack错误-无法解决加载程序在‘tmp/构建.’
EN

Stack Overflow用户
提问于 2017-08-21 04:00:29
回答 3查看 2K关注 0票数 0

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

代码语言:javascript
复制
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,在那里我得到了一个更有帮助的错误:

代码语言:javascript
复制
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中的脚本和其他相关的部分

代码语言:javascript
复制
"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

代码语言:javascript
复制
"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

代码语言:javascript
复制
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")
        })
    ]
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-21 05:51:17

感谢@Marek和@考古队在这方面的想法-我想出了一个解决办法,虽然它可能不是最佳的.

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

票数 2
EN

Stack Overflow用户

发布于 2017-12-19 12:49:21

我也有同样的问题,但对我来说,这不是以这样的方式解决的。

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

这就是解决办法:

代码语言:javascript
复制
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")
]
票数 2
EN

Stack Overflow用户

发布于 2017-08-21 04:51:18

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45789115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档