首页 > 解决方案 > Node.js vercel/pkg express 'return 0 error' 和 fastify 错误。错误:编译阶段文件或文件夹未包含在可执行文件中

问题描述

当尝试将您的节点项目编译为可执行文件并且您使用 express 进行路由时,它可能会导致如下所示的错误:

john@john:~/Tofa/Projects/Convert node project into .exe/Secondtest/express$ ./express
Error: File or directory '/**/express/views/index.html' was not included into executable at compilation stage. Please recompile adding it as asset or script.
at error_ENOENT (pkg/prelude/bootstrap.js:539:17)
at findNativeAddonForStat (pkg/prelude/bootstrap.js:1201:32)
at statFromSnapshot (pkg/prelude/bootstrap.js:1224:25)
at Object.stat (pkg/prelude/bootstrap.js:1250:5)
at SendStream.sendFile (/snapshot/express/node_modules/send/index.js:721:6)
at SendStream.pipe (/snapshot/express/node_modules/send/index.js:595:8)
at sendfile (/snapshot/express/node_modules/express/lib/response.js:1103:8)
at ServerResponse.sendFile (/snapshot/express/node_modules/express/lib/response.js:433:3)
at /snapshot/express/index.js:21:9
at Layer.handle [as handle_request] (/snapshot/express/node_modules/express/lib/router/layer.js:95:5)

index.js代码(app起点)如下图:

/*jshint strict:false */

(function() {
    'use strict';
    // this function is strict...
}());

const express = require('express');
const app = express();
const Server = require('http').Server;
const server = new Server(app);

server.listen(8080);

// __dirname is used here along with package.json.pkg.assets
// sepkg .e https://github.com/zeit/pkg#config and
// https://github.com/zeit/pkg#snapshot-filesystem
app.use('/', express.static(__dirname + '/views'));

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/views/index.html');
});

错误的可能原因是什么?你如何在expressor中解决它fastify

标签: javascriptnode.jsexpresspathfastify

解决方案


解决方案

您的项目可能有两种资产:

  • 本地文件:您想使用由 pkg 捆绑的文件(必须在构建时可用)。在这里,您可以使用相对路径,如., .., __dirname,__filename等。
  • 远程文件:您想使用构建时不可用的文件(稍后下载,用户上传等)。在这里你不能使用相对路径。相反,您必须使用process.cwd()或其他在运行时派生路径的函数。

表示

当尝试将您的节点项目编译为可执行文件并且您使用 express 进行路由时,它可能会导致如下所示的错误:

john@john:~/Tofa/Projects/Convert node project into .exe/Secondtest/express$ ./express
Error: File or directory '/**/express/views/index.html' was not included into executable at compilation stage. Please recompile adding it as asset or script.
at error_ENOENT (pkg/prelude/bootstrap.js:539:17)
at findNativeAddonForStat (pkg/prelude/bootstrap.js:1201:32)
at statFromSnapshot (pkg/prelude/bootstrap.js:1224:25)
at Object.stat (pkg/prelude/bootstrap.js:1250:5)
at SendStream.sendFile (/snapshot/express/node_modules/send/index.js:721:6)
at SendStream.pipe (/snapshot/express/node_modules/send/index.js:595:8)
at sendfile (/snapshot/express/node_modules/express/lib/response.js:1103:8)
at ServerResponse.sendFile (/snapshot/express/node_modules/express/lib/response.js:433:3)
at /snapshot/express/index.js:21:9
at Layer.handle [as handle_request] (/snapshot/express/node_modules/express/lib/router/layer.js:95:5)

该错误源于 pkg 无法识别快速路由中使用的路径模式。因此,如果您的初始路线看起来像此index.js文件中的路线:

/*jshint strict:false */

(function() {
    'use strict';
    // this function is strict...
}());

const express = require('express');
const app = express();
const Server = require('http').Server;
const server = new Server(app);

server.listen(8080);

// __dirname is used here along with package.json.pkg.assets
// sepkg .e https://github.com/zeit/pkg#config and
// https://github.com/zeit/pkg#snapshot-filesystem
app.use('/', express.static(__dirname + '/views'));

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/views/index.html');
});

res.sendFile(__dirname + '/views/index.html');pkg 不起作用的地方。

直接连接路径是一种不好的编程习惯。

解决问题不要__dirname直接使用,使用path.join或者新建一个函数来解决这个问题,如下图:

function getDirPath() {
  if (process.pkg) {
    return path.resolve(process.execPath + "/..");
  } else {
    return path.join(require.main ? require.main.path : process.cwd());
  }
}

并将其替换在代码中__dirname,获取代码如下所示:

/*jshint strict:false */

(function() {
    'use strict';
    // this function is strict...
}());

// Setting up our app requirements

const express = require('express');
const app = express();
const Server = require('http').Server;
const server = new Server(app);
const path = require('path');

// Setting up our port

server.listen(5000);

// Configuiring simple express routes
// getDir() function is used here along with package.json.pkg.assets

app.use('/', express.static(getDir() + '/views'));

app.get('/', function(req, res) {
    res.sendFile(getDir() + '/views/index.html');
});


// Using a function to set default app path
function getDir() {
    if (process.pkg) {
        return path.resolve(process.execPath + "/..");
    } else {
        return path.join(require.main ? require.main.path : process.cwd());
    }
}

不要忘记path在代码的开头要求。

斋戒

如果使用 fastify,你可以使用:

const resolve = require('path').resolve 
const absolutePath = resolve('./')

推荐阅读