我正在尝试使用我在打字稿中编写的客户端javascript库中使用把手,当我使用import * as Handlebars from 'handlebars'
时,我收到一条错误消息,提示打字稿“找不到模块打字稿”
我尝试导入import * as Handlebars from 'handlebars/runtime'
而不是仅仅handlebars
算不上运气。
我发现了类似的问题here,并尝试将把手替换添加到我的tsconfig文件中,这无助于查找模块
我认为指定我正在运行umd编译很重要的原因是,如果将其设置为commonjs编译,那么找到该引用似乎没有问题,但是仅推荐我进行了commonjs的研究当您想将该库用作Node.js应用程序的一部分(可能在服务器环境中运行)时,看到这是我正在创建的客户端库,尽管有人可能会认为这不是合适的解决方案,能够证明我错了。以umd为目标似乎同时提供了commonjs和amd编译,因此我认为这将是“两全其美”的解决方案
tsconfig:
{
"compilerOptions": {
"target": "es5",
"module": "umd",
"strict": true,
// "paths": {
// "handlebars": ["handlebars/dist/handlebars.min.js"]
// },
"esModuleInterop": true
}
}
package 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("");
}
这里的预期结果是使用把手作为我的库的一部分。
答案 0 :(得分:1)
我很幸运import Handlebars from "handlebars/dist/handlebars.js"