[英]Node.js - Create executable with PKG
我在 node.js 中有一個應用程序,現在我正在嘗試創建一個可執行文件來在沒有所有項目文件的情況下運行該應用程序,但是當我嘗試使用pkg
( https://github.com/zeit/pkg#檢測源代碼中的資產)。
在package.json
我添加這個:
"pkg": {
"scripts": "public/js/*.js",
"assets": [
"views/**/*"
],
"targets": "node6"
},
在控制台中,我運行此命令,並且在此過程中沒有任何錯誤並創建了 3 個平台可執行文件pkg index.js --output
當我運行可執行文件時,它開始時沒有錯誤,當我訪問瀏覽器時,它會返回此錯誤:
Error: Failed to lookup view "login" in views directory "/snapshot/Picking/views"
at EventEmitter.render (/snapshot/Picking/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/snapshot/Picking/node_modules/express/lib/response.js:1008:7)
at ServerResponse.app.use.res.render (/snapshot/Picking/index.js:0)
at index (/snapshot/Picking/controllers/loginController.js:0)
at Layer.handle [as handle_request] (/snapshot/Picking/node_modules/express/lib/router/layer.js:95:5)
at next (/snapshot/Picking/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/snapshot/Picking/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/snapshot/Picking/node_modules/express/lib/router/layer.js:95:5)
at /snapshot/Picking/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/snapshot/Picking/node_modules/express/lib/router/index.js:335:12)
在 index.js 我有這一行來訪問視圖文件夾:
app.set("views", path.join(__dirname, 'views'));
我該如何解決這種情況?
謝謝
您將資產路徑設置為“腳本”,但必須將此路徑設置為“資產”,如下所示:
"pkg": {
"assets": [
"views/**/*",
"public/js/*.js"
],
"targets": "node6"
因為 node.js 文件的“腳本”。 並且您的應用程序未找到前端 js 文件。
你不能使用app.set("views", path.join(__dirname, 'views'));
因為pkg
將您的視圖放置在快照目錄中,所以您的項目將找不到它。
您必須在渲染時明確指出您的視圖所在的位置:
res.render(path.join(__dirname, '..', 'views/index'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.