简体   繁体   English

在TypeScript中需要JavaScript Node.js模块(未设置allowJs')

[英]Requiring a JavaScript Node.js module in TypeScript (allowJs' is not set)

I have an Angular2 app inside Electron. 我在Electron里面有一个Angular2应用程序。 Now, I would like to use the @pokusew/pcsclite library to use NFC functionality. 现在,我想使用@pokusew/pcsclite库来使用NFC功能。 This library uses native Node.js modules. 该库使用本机Node.js模块。

When I try to require the library in my component.ts like this: 当我尝试在我的component.ts中要求库时,如下所示:

declare var pcsclite: any;
var pcsclite = require('../../../node_modules/@pokusew/pcsclite/');

I get and error that says: 我得到并且错误说:

error TS6143: Module '../..' was resolved to '../../lib/pcsclite.js', but '--allowJs' is not set. 错误TS6143:模块'../ ..'被解析为'../../lib/pcsclite.js',但未设置' - allowJs'。

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: 另一方面,如果我尝试通过index.html中的<\\ script> -Tag导入库,我会收到一条错误消息:

ZoneAwareError Error: Could not locate the bindings file. ZoneAwareError错误:找不到绑定文件。 Tried:... 尝试:...

Finally, if I var pcsclite = require('@pokusew/pcsclite'); 最后,如果我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. main.js ,然后它可以工作,但是我无法从我的Angular应用程序中访问它。

Add the allowJs option in your tsconfig.json like this: 添加allowJs选项在tsconfig.json这样的:
as fabian lauer said also add outDir option to specify where your compiled files will be: 正如fabian lauer所说,还添加了outDir选项来指定编译文件的位置:

{
    "compilerOptions": {
        "outDir": "./built", <--- add this
        "allowJs": true,  <--- and this
        "target": "es5"
    },
    "include": [
        "./src/**/*"
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM