javascript - 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');
});
错误的可能原因是什么?你如何在express
or中解决它fastify
?
解决方案
解决方案
您的项目可能有两种资产:
- 本地文件:您想使用由 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('./')
推荐阅读
- sql - 排名数据(降序)
- unity3d - Unity Vector Raycast 调试
- sql - 如果不明确 Id,替换前导零将不起作用
- c# - MissingMethodException PostAsJsonAsync 异常
- julia - f(x::Array{Real}) 不包括 Julia 中的 f(x::Array{Float64})
- python - Python,从生成器产生的紧凑方法
- android - 调整 Android 以并行运行应用程序
- wso2 - Liferay 7.1 OpenID Connect 作为默认登录
- php - 如何使用 PHP 7.0 访问元素 _product
- javascript - 将 2D 圆表示为 1D 数组(出租车几何,冯诺依曼邻域)