在TypeScript中需要JavaScript Node.js模块(未设置allowJs') [英] Requiring a JavaScript Node.js module in TypeScript (allowJs' is not set)
问题描述
我在Electron里面有一个Angular2应用程序。现在,我想使用 @pokusew / pcsclite
库来使用NFC功能。这个库使用本机Node.js模块。
I have an Angular2 app inside Electron. Now, I would like to use the @pokusew/pcsclite
library to use NFC functionality. This library uses native Node.js modules.
当我尝试需要我的component.ts中的库时,这样:
When I try to require the library in my component.ts like this:
declare var pcsclite: any;
var pcsclite = require('../../../node_modules/@pokusew/pcsclite/');
我得到并且错误说:
错误TS6143:模块'../ ..'被解析为'../../lib/pcsclite.js',但' - allowJs'未设置。
error TS6143: Module '../..' was resolved to '../../lib/pcsclite.js', but '--allowJs' is not set.
另一方面,如果我尝试通过index.html中的< \script> -Tag导入库,我会收到错误这说:
On the other hand, if I try to import the library via a <\script>-Tag in the index.html I get an error that says:
ZoneAwareError 错误:找不到绑定文件。试过:...
ZoneAwareError Error: Could not locate the bindings file. Tried:...
最后,如果我 var pcsclite = require('@ pokusew / pcsclite') ;
在 main.js
中,然后它可以工作,但是我无法从我的Angular应用程序中访问它。
Finally, if I var pcsclite = require('@pokusew/pcsclite');
in the main.js
, then it works, but then I don't have access to it from inside my Angular app.
推荐答案
在 tsconfig.json中添加
像这样: allowJs
选项
作为 fabian lauer 说也添加 outDir
选项,用于指定编译文件的位置:
Add the allowJs
option in your tsconfig.json
like this:
as fabian lauer said also add outDir
option to specify where your compiled files will be:
{
"compilerOptions": {
"outDir": "./built", <--- add this
"allowJs": true, <--- and this
"target": "es5"
},
"include": [
"./src/**/*"
]
}
这篇关于在TypeScript中需要JavaScript Node.js模块(未设置allowJs')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!