【问题标题】:How to fix handlebars 'module not found' in typescript when compiling to umd编译为 umd 时如何在打字稿中修复车把“找不到模块”
【发布时间】:2019-05-12 20:46:01
【问题描述】:

我正在尝试在我用打字稿编写的客户端 JavaScript 库中使用把手,当我使用 import * as Handlebars from 'handlebars' 时,我收到一条错误消息,提示打字稿“找不到模块打字稿”

我尝试导入 import * as Handlebars from 'handlebars/runtime' 而不是仅仅导入 handlebars 没有运气。

我发现了一个类似的问题here 并尝试将把手替换添加到我的 tsconfig 文件中,这对查找模块没有帮助

我觉得指定我正在运行 umd 编译很重要的原因是,如果我将其设置为 commonjs 编译,那么找到该参考似乎没有问题,但从我执行的研究来看,仅推荐 commonjs当您想将该库用作 nodejs 应用程序的一部分(可能在服务器环境中运行)时,因为这是我正在创建的客户端库,我不相信这是一个合适的解决方案,尽管有人可能是能够证明我错了。以 umd 为目标似乎同时提供 commonjs 和 amd 编译,所以我认为这将是一个“两全其美”的解决方案

tsconfig:

{
  "compilerOptions": {
    "target": "es5",
    "module": "umd",
    "strict": true,
    // "paths": {
    //   "handlebars": ["handlebars/dist/handlebars.min.js"]
    // },
    "esModuleInterop": true   
  }
}


包json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "handlebars": "^4.1.2"
  }
}

main.ts:

import * as Handlebars from 'handlebars'

export function hello() {
    Handlebars.compile("");
}

这里的预期结果是使用把手作为我图书馆的一部分。

【问题讨论】:

  • 我也有同样的问题。谢谢你已经问过了。

标签: typescript npm umd


【解决方案1】:

我很幸运import Handlebars from "handlebars/dist/handlebars.js"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2021-01-07
    • 2013-12-30
    相关资源
    最近更新 更多