【问题标题】:nestjs node module issue, How to use when/sequence module in nestjsnestjs 节点模块问题,如何在nestjs 中使用when/sequence 模块
【发布时间】:2021-10-27 05:41:08
【问题描述】:

我在nestjs 中编写代码。我在我的项目中使用了 when 模块 (https://www.npmjs.com/package/when)。

我在导入时模块如下

import { sequence } from "when/sequence";

但是当我打印序列时。它给出未定义的值。出于这个原因,无法在nestjs中使用模块时 这里知道如何在nestjs中导入和安装节点模块

【问题讨论】:

  • 这个包的最后一个版本是在 2017 年,哈哈,我想你可以替换它

标签: node.js nestjs


【解决方案1】:

好像whenonly compiles to CommonJs and AMD.,因此它不允许ES6中引入的import .. from "..."语法。

您应该使用require() 导入它。

const sequence = require("when/sequence")

【讨论】:

  • 同样的问题。我正在使用节点 14.17.5。这里还有其他方法吗?
  • 我刚刚编辑了答案。该库不允许 ES6 导入
  • 它正在工作
  • @SurajDalvi 但您可以在不使用 ESM 的情况下使用节点 14.17.5。但是如果你想使用 ESM,那么阅读这个nodejs.org/api/module.html#modulecreaterequirefilename
【解决方案2】:
import { sequence } from "when/sequence.js";

或者使用dynamic imports加载CJS模块:

const { default: sequence } = await import("when/sequence.js");

【讨论】:

    猜你喜欢
    • 2021-08-31
    • 2020-09-30
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2020-06-30
    • 1970-01-01
    相关资源
    最近更新 更多