Error: Cannot find module 'pug'
Hello.I generated an express-app using the express-generator and specified its view to be pug.Now I want to package it using the following command: pkg ./bin/www.This generates the executable files for each platform.However when I try to run my app and load my server through the browser I get the following error:
Error: Cannot find module 'pug'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
at Function.Module._resolveFilename (module.js:534:15)
at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1269:46)
at Function.Module._load (module.js:464:25)
at Module.require (module.js:577:17)
at Module.require (pkg/prelude/bootstrap.js:1153:31)
at require (internal/module.js:11:18)
at new View (/snapshot/work-calendar/node_modules/express/lib/view.js:79:30)
at Function.render (/snapshot/work-calendar/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/snapshot/work-calendar/node_modules/express/lib/response.js:971:7)
at /snapshot/work-calendar/app.js:0:0
Here is my package.json:
`
{
"name": "work-calendar",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"help": "pkg --help",
"package-linux": "pkg .-t latest-linux-x64 -o ./app ./app.js",
"p-all": "pkg ./bin/www",
"p-win":"pkg . -t node8-win-x64"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"morgan": "~1.9.0",
"pug": "2.0.0-beta11",
"serve-favicon": "~2.4.5"
},
"pkg": {
"assets": "views/**/*"
},
"devDependencies": {
"pkg": "^4.3.1"
}
}
`
Any ideas how can I resolve it?
How are you requiring pug?
anyone has solved this issue ??
It would help to see some code.
@HardlyMirage i have done the same, if you create an express app from the express generator, you will have a "View" folder with some files having the extension ".pug"; example: "index.pug"...
the thing is, in zeit pkg, we arent able to include modules in the binary file( .exe). please confirm.
I cloned a starter kit: https://www.npmjs.com/package/express-pug-starter
Added "bin": "server/index.js" to package.json.
Then ran pkg . --target node8-macos-x64
./express-pug-starter started the project without any issues and the UI works fine too.
Without seeing your code, I cannot help you.
Just tested with express generator as well.
npm install express-generator -g
express --view=pug express-pug-generated
cd express-pug-generated/
vim package.json
# Add the following two properties:
# "bin": "bin/www",
# "pkg": {
# "assets":
# [
# "views/**/*",
# "public"
# ]
# },
npm install
pkg .
./express-pug-generated-macos
The application runs just fine on port 3000.
@HardlyMirage I will test it later on today and will let you know if that works for ubuntu :) Anyway thanks for the assistance
@kounelios13 Eazy, the solution to this would be:
in package.json add
{
"main": "app.js",
"bin": "app.js",
......
"scripts": {
"build-package": "pkg package.json"
},
.....
"pkg": {
"assets": [
"views/**/*"
],
"targets": [
"node8"
]
}
}
make sure you add main and bin and the script "build-package" and the "pkg" referencing the views folder(which holds the pug templates).
next, add in your app.js right before
app.listen(3000, () => console.log('Example app listening on port 3000!'));
right before module.exports = app;
There we are not depending on "www" file to host our app.
Our entry point would be in this case app.js,(if you wanna run the app through npm start, it will fail because in package.json says the entry point is www, whereas we are re-hosting the app in app.js)
So in order to run the app(for testing purposes) run it with node app.js
and finally, to get an executable of your app, simple type in the command:
npm run build-package
Please feel free to ask any question.
Thanks.
Do you have Pug installed?
Seems to have been answered