问题
I keep getting this error when deploing to Heroku. Build process was succesfull but then I get this error. Can't figure out what is the problem and shouldn't path be src/server/views? Everything is working locally.
Error: Failed to lookup view "index" in views directory "src\server/views"
[web.1]:at /app/node_modules/express/lib/router/index.js:281:22
app[web.1]:at param (/app/node_modules/express/lib/router/index.js:354:14)
app[web.1]:at Function.render(/app/node_modules/express/lib/application.js:580:17)
app[web.1]:at param (/app/node_modules/express/lib/router/index.js:365:14)
app[web.1]:at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
app[web.1]:at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
I use webpack tu bundle my server side code and I use html-webpack-plugin to inject my index.ejs template from views folder to webpack bundle.
webpack.config.server.js
module.exports = {
devtool: 'source-map',
performance: {
hints: false
},
target: 'node',
node: {
__dirname: true,
__filename: true
},
entry: {
bundle: './src/server/prodServer.js'
},
output: {
path: path.join(buildPath, 'build'),
filename: '[name].js'
}
...
plugins: [
new HtmlWebpackPlugin({
template: 'ejs-loader!./src/server/views/index.ejs'
})
]
prodServer.js
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
folder structure
as you can see I have index.ejs inside views folder. prodServer is my server file and after webpack build I get bundle file and index.html.
In Heroku start script I run node ./src/build/bundle.js
回答1:
It is because of how your __dirname is defined in Heroku. If you run heroku run bash and then run pwd you will see that your current working directory is /app, hence why /app is added in front of all your paths.
Try to play around with the command ls in your bash to see if your server folder is listed, and then with a cd server command see if the views file is there as well. Note that it is case sensitive, so most likely you had it defined in uppercase.
Also, note that it adds a src\ (with a backward slash instead of forward slash) to your src\server/views path, which could be from a wrong configuration in your webpack.config.server.js file.
来源:https://stackoverflow.com/questions/44710767/heroku-error-error-failed-to-lookup-view-index-in-views-directory