Ncc: @now/node error: Cannot find module 'pug'

Created on 20 Mar 2019  路  5Comments  路  Source: vercel/ncc

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.

bug dynamic require package issue

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.

All 5 comments

@guybedford I was able to reproduce this issue.

Steps to reproduce

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

Error

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)

Workaround

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:

  • If there is a require to "express"
  • And that require is called as a function and assigned to another variable
  • And that instance has app.set("view engine", ...) called, then include the line 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

j picture j  路  3Comments

rauchg picture rauchg  路  3Comments

guybedford picture guybedford  路  5Comments

hrueger picture hrueger  路  4Comments

drewolson picture drewolson  路  3Comments