Webpack: ENOENT: No such file or directory, open 'head.ejs'
I have the following webpack.config.js
file:
'use strict';
let path = require('path');
let webpack = require('webpack');
let HtmlWebpackPlugin = require('html-webpack-plugin');
let ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: [
'./index.js'
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/'
},
devtool: 'inline-source-map',
watch: true,
module: {
rules: [
{
test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: [ 'style-loader', 'css-loader?modules', ],
publicPath: '/dist'
})
},
{
test: /\.ejs$/,
use: 'ejs-compiled-loader'
}
],
},
plugins: [
new ExtractTextPlugin({
filename: 'styles.css'
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
minify: {
collapseWhitespace: true
},
hash: true,
template: 'ejs-render-loader!./views/index.ejs'
}),
]
};
When trying to load an ejs file containing <% somefile %>
, the file could not be found .. this is the error I am getting:
Child html-webpack-plugin for "index.html":
Asset Size Chunks Chunk Names
index.html 26.2 kB 0
chunk {0} index.html 554 bytes [entry]
[./node_modules/ejs-render-loader/index.js!./views/index.ejs] ./~/ejs-render-loader!./views/index.ejs 554 bytes {0} [built] [failed] [1 error]
ERROR in ./~/ejs-render-loader!./views/index.ejs
Module build failed: Error: ENOENT: no such file or directory, open 'head.ejs'
at Object.fs.openSync (fs.js:584:18)
at fs.readFileSync (fs.js:491:33)
at Object.exports.parse (/var/www/rio-olympics-3/monitor/node_modules/ejs-compiled-loader/node_modules/ejs/lib/ejs.js:168:19)
at Object.exports.compile (/var/www/rio-olympics-3/monitor/node_modules/ejs-compiled-loader/node_modules/ejs/lib/ejs.js:245:15)
at Object.module.exports (/var/www/rio-olympics-3/monitor/node_modules/ejs-compiled-loader/index.js:7:22)
webpack: Failed to compile.
I tried many file path formats and none of them worked, here is my ejs file with my head.ejs in the same folder:
<!DOCTYPE html>
<html lang="en">
<% include head %>
<body>
<div id="navbar-app"></div>
<p> Welcome, more coming soon! </p>
</body>
<!-- insert component scripts below here -->
<script src="dist/js/NavBarMain.js"></script>
</html>
source to share
You have two different bootloaders configured for index.ejs
what causes the collision.
General rule for everyone .ejs
:
{
test: /\.ejs$/,
use: 'ejs-compiled-loader'
}
Inline rule for template in html-webpack-plugin
:
template: 'ejs-render-loader!./views/index.ejs'
If you want to use the built-in loader, you can add a presenter !
, so the webpack won't apply other loaders:
template: '!ejs-render-loader!./views/index.ejs'
The above issue has been fixed, but it's a bit doubtful why you have two different bootloaders ejs
. To be honest, I don't quite understand the differences in uploaders, since both point to the same GitHub repository , although it seems to me it ejs-render-loader
's just an earlier version. They actually handle different options, which is why you get the error.
From example in Usageejs-compiled-loader
:
// Child Templates
// path is relative to where webpack is being run
<%- include templates/child -%>
This means that if you want to use ejs-compiled-loader
for everyone .ejs
, you need to change include:
<% include views/head %>
And you can get rid of the built-in bootloader in html-webpack-plugin
:
template: './views/index.ejs'
It's up to you if you want to change this, I'll just try to avoid bootloader conflicts and I don't think there ejs-render-loader
will be any more updates.
In fact, there is an v2.2.0
of ejs-compiled-loader
that falls back to another, includes behavior with respect to the current file, which is more intuitive. You can install it with:
npm install --save-dev ejs-compiled-loader@2.2.0
source to share