node.js - 在 Angular typescript 文件中导入 Node 模块时找不到模块错误

标签 node.js angular typescript electron

我正在尝试创建可以在 MacOS 上管理 Airplay 的 Electron 应用程序

我正在使用 Angular 和 TypeScript 来 package 此 npm 包中的 API Airplay npm package :

这是我在 TypeScript 中使用的代码:

export class AirplaySharing {
    public init() {
        const airplayer = window['require']('airplayer');
        const list = airplayer();

        list.on('update', function(player) {
            console.log('Found new AirPlay device:', player.name);
        });
    }
}

我在调用 init() 时收到此错误

Error: Cannot find module 'airplayer'
at Module._resolveFilename
at Function.Module._resolveFilename
at Function.Module._load
at Module.require
at require
at t.init
at new t
at bs
at gs
at Ys

最佳答案

这行得通吗

import airplayer = require('airplayer');

export class AirplaySharing {
    public init() {
        const list = airplayer();
        list.on('update', function(player) {
            console.log('Found new AirPlay device:', player.name);
        });
    }
}

此外,使用 typescript 构造函数会更好吗?像这样:

import airplayer = require('airplayer');

export class AirplaySharing {
    constructor(public list: airplayer()) {
        list.on('update', function(player) {
            console.log('Found new AirPlay device:', player.name);
        });
    }
}

希望有帮助:)

关于node.js - 在 Angular typescript 文件中导入 Node 模块时找不到模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936499/

相关文章:

javascript - throttle 功能打破了我的输入实时搜索

node.js - 如何使用 Intl 来 brew install node.js?

node.js - 值被添加到数据库条目的末尾,而不是更新相关列

node.js - 无法通过中间件检索添加到请求对象的值

comparison - Erlang 相对于(类似于)node.js 的优势?

typescript - 使用 Typescript 的 angular2 上的开关按钮

angular - 括号在页面刷新时破坏 Angular 路由

reactjs - 基于 Typescript 的 Reactjs 中的三元运算符无法按我的预期工作

angular - 如何在 Angular 中调整 .css 文件的导入顺序

TypeScript:将 `Pick<T, K>` 分配给 `Partial<T>`