[英]Webpack error - can't resolve loader in 'tmp/build…'
我最近将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")
})
]
}
感谢@Marek和@ archae0pteryx在此方面的想法-我想出了一个解决方法,尽管它可能不是最佳选择...
基本上,我创建了一个webpack.dev.config.js
文件,其中包含对eslint-loader
的引用……这很好,因为我只想在本地开发中使用linting。 然后,我从更新后的主配置文件package.json
删除了这些引用,以在本地开发中使用dev config,这一切都很好。
我遇到了同样的问题,但是对我来说,这并没有解决。
我用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")
]
我认为您的问题可能是您在devDependencies中devDependencies
。 如果我没记错的话,Heroku只会安装您应用程序的dependencies
项。 尝试将EsLint移入dependencies
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.