【问题标题】:How to install node-win-shortcut by using npm如何使用 npm 安装 node-win-shortcut
【发布时间】:2019-07-22 05:13:27
【问题描述】:

我正在尝试使用 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 的安装没有完全完成,一些构建过程仍然存在。

【问题讨论】:

  • 你能显示代码吗?对于快捷方式,您应该使用 squirrel 。
  • 添加整个文件可能与此处无关。
  • 这似乎是与 Typescript 相关的问题。 @types/node-win-shortcut 是否存在?
  • @SeanKelly 不,它不存在。我尝试安装,它说找不到包。
  • @SwapnilRustagi 你需要包的常量应该是一个变量。 (即 var shortcut = (require 'node_win_shortcut'); 试试看!

标签: node.js npm electron npm-install


【解决方案1】:

看起来您正在使用 TypeScript。当你想使用 TypeScript 中的 JS 库时(使用像 noImplicitAny 这样的严格检查),你必须拥有你想要使用的模块的类型声明。 GitHub 上的 DefinitelyTyped 项目为 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)并将其导入到您需要的地方,您应该可以开始了。

如果由于任何原因无法正常工作,您可以使用 DefinedTyped 类型声明作为参考,供您自己建模,例如 this very simple one 用于 is-number 包。


作为单独的说明,您在 JavaScript 代码中看不到方法本身的原因是绑定实际上是来自本机代码 you can see herebuilt on installation(正如您可以想象的那样,在 Windows 上创建快捷方式需要必须在 Windows 系统上运行的本机代码绑定——npm i node-win-shorcut 在 Linux 或 MacOS 上会失败)。您可以查看node_modules/node-win-shortcut 目录以查看从index.js 文件引用的文件中的构建工件。

【讨论】:

  • 非常感谢您的解释。我会尽快尝试。
猜你喜欢
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 2022-10-06
  • 2017-11-30
  • 2011-10-04
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多