【问题标题】:Dynamic import - import() - fails when the code is packaged into an executable动态导入 - import() - 代码打包成可执行文件时失败
【发布时间】:2021-10-27 07:50:46
【问题描述】:

我在 commonjs 环境中工作,试图动态导入 es 模块

考虑下面的代码:

const mysqlController = (async function () {
    try{
        var {default:dateformat}= await import('dateformat');
        // also tried await import('../../../node_modules/dateformat/lib/dateformat.mjs')
    }
    catch(e){
        console.error('Line 31 of db_controller');
        console.error(e);
    }
// More stuff
})()

await import('dateformat'); 语句在调试时工作正常。但是当使用pkg 生成一个独立的可执行文件时,它给出了以下错误:

TypeError: Invalid host defined options

谁能告诉我这是怎么回事?

【问题讨论】:

    标签: node.js import


    【解决方案1】:

    好的,由于某些原因,使用包 pkg 生成的节点可执行文件不支持 import() 语句。

    作为临时解决方法,我为尝试使用rollup 导入的es 模块制作了cjs 替代品。 然后,我将包复制到本地并required 那些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2020-11-13
      • 2021-08-02
      相关资源
      最近更新 更多