When using the stable or canary builder, express deployments that depend on pug will run into the following error: Error: Cannot find module 'pug'
To reproduce this issue, I made a repo that you can clone and deploy: https://github.com/coetry/wtf-pug
Note that i've included a test-server.js file, which works fine locally. Also note that this is not an ncc error because if you run ncc build index.js && cp test-server.js dist && cd dist && node test-server.js, it will work as expected. This error is only encountered when deploying.
@guybedford I was able to reproduce this issue.
git clone https://github.com/coetry/wtf-pug
cd wtf-pug
npm install
ncc build test-server.js
rm -rf node_modules # this step is important
node dist/index.js
When you visit http://localhost:3000 you'll see the following error
Error: Cannot find module 'pug'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at new View (/Users/styfle/Desktop/foobar/wtf-pug/dist/index.js:16535:14)
at Function.render (/Users/styfle/Desktop/foobar/wtf-pug/dist/index.js:3765:12)
at ServerResponse.render (/Users/styfle/Desktop/foobar/wtf-pug/dist/index.js:13687:7)
at module.exports.318.app.get (/Users/styfle/Desktop/foobar/wtf-pug/dist/index.js:6966:7)
at Layer.handle [as handle_request] (/Users/styfle/Desktop/foobar/wtf-pug/dist/index.js:3942:5)
at next (/Users/styfle/Desktop/foobar/wtf-pug/dist/index.js:14058:13)
There is a workaround to get this to work as expected thanks to @TooTallNate 馃檶
Our users would never think about this solution so it's a hack to get around the dynamic require.
--- a/index.js
+++ b/index.js
@@ -3,6 +3,7 @@ const path = require("path");
const app = express();
+app.engine("pug", require("pug").__express);
app.set("view engine", "pug");
app.set("views", path.join(__dirname, "views"));
@guybedford Is this fixable? (related to #254)
The fix here sounds exactly right to me.
The only alternative we have would be to explicitly support this ExpressJS usage pattern that is:
app.engine("pug", require("pug").__express);This would be a bit of work to integrate and would be only for this use case, but it would be possible to ensure this exact use case is fixed.
@guybedford Let's solve this for all express templates.
Since express expects the default export to be __express from the template, I think it can be fixed for all template engines, not just pug.
Express is probably the most popular node.js http framework so we need to have this working in ncc.
I agree, solving this for all express template engines would be ideal.
Most helpful comment
@guybedford Let's solve this for all express templates.
Since express expects the default export to be __express from the template, I think it can be fixed for all template engines, not just
pug.Express is probably the most popular node.js http framework so we need to have this working in
ncc.