如何使用npm安装node-win-shortcut

时间:2019-02-28 10:11:18

标签: node.js npm electron npm-install

我正在尝试使用electron-windows-notifications模块将本机Windows通知添加到Zulip Electron。

因此,我正在阅读https://github.com/felixrieseberg/electron-windows-notifications/blob/master/samples/shortcut.js上给出的示例代码,该示例代码为应用创建了快捷方式,并且是通知正常工作所必需的。

下面也提供了代码:

const shortcut = require('node-win-shortcut')
const appId = 'electron-windows-notifications'

shortcut.createShortcut(process.execPath, 'node', appId)

我运行了npm install node-win-shortcut以安装该软件包。但是,当我将const shortcut = require('node-win-shortcut');添加到文件中时,VS Code显示错误:

Could not find a declaration file for module 'node-win-shortcut'. '.../zulip-electron/node_modules/node-win-shortcut/index.js' implicitly has an 'any' type. Try 'npm install @types/node-win-shortcut' if it exists or add a new declaration (.d.ts) file containing 'declare module 'node-win-shortcut'';

这确实是正确的,因为node-win-shortcut/index.js只有:

module.exports = require('./build/Release/node_win_shortcut_bindings.node');

我不知道如何访问createShortcut()方法。我怀疑node-win-shortcut的安装尚未完全完成,并且仍有一些构建过程。

1 个答案:

答案 0 :(得分:3)

您似乎正在使用TypeScript。如果要使用TypeScript中的JS库(使用严格的检查,例如noImplicitAny),则必须具有要使用的模块的类型声明。 DefinitelyTyped上的GitHub项目为NPM软件包提供了类型声明文件,它们不是独立存在的,而是在NPM上的@types organization下发布的。如果某个程序包未附带类型定义,则可以在here上查找它,以查找是否有它们的程序包。

但是,您所需的软件包似乎没有包括在内。您可以做的是编写自己的类型定义,以便可以在TypeScript中使用此库。仅涵盖所需方法的最小类型声明如下所示:

declare module 'node-win-shortcut' {
  function createShortcut(path: String, name: String, appId: String): void
}

如果将此类型声明放在文件(即node-win-shorcut.d.ts)中,然后将其导入到需要的位置,那么您应该会很方便。

如果由于某种原因该方法不起作用,则可以使用DefinitelyTyped类型声明作为自己建模的参考,例如this very simple one用于is-number包。


另外,您在JavaScript代码中看不到方法本身的原因是,绑定实际上是built on installation与本地代码you can see here的绑定(您可以想象, Windows上的快捷方式要求必须在Windows系统上运行本机代码绑定-npm i node-win-shorcut在Linux或MacOS上会失败)。您可以在node_modules/node-win-shortcut目录中查看从index.js文件引用的文件中的构建工件。