Node.js vercel/pkg 表示"返回 0 错误"并快速处理错误。错误:在编译阶段未包含在可执行文件中的文件或文件夹



当试图将节点项目编译为可执行文件,并且您正在使用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代码(应用程序起点(如下所示:

/*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');
});

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

解决方案

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

  • 本地文件:您希望使用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());
}
}

并将其替换为CCD_ 12所在的代码,得到如下代码:

/*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

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

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

相关内容

  • 没有找到相关文章

最新更新