【问题标题】:Node.js vercel/pkg express 'return 0 error' and fastify errors. Error: File or folder not included into executable during compilation stageNode.js vercel/pkg express 'return 0 error' 和 fastify 错误。错误:编译阶段文件或文件夹未包含在可执行文件中
【发布时间】:2021-08-30 02:46:14
【问题描述】:

当您尝试将您的节点项目编译为可执行文件并且您使用 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');
});

错误的可能原因是什么? 在expressfastify 怎么解决?

【问题讨论】:

    标签: javascript node.js express path fastify


    【解决方案1】:

    解决方案

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

    • 本地文件:您要使用由 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('./')
    

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2012-08-27
      相关资源
      最近更新 更多