[英]Could not find a declaration file for module 'drivelist'
I know there are a couple of similar questions about declaration files but none of them could help me fix my problem. 我知道关于声明文件有几个类似的问题,但没有一个可以帮助我解决我的问题。 First of all I am going to explain why I am trying to do this. 首先,我要解释为什么我要这样做。 If you just want to read the actual problem start reading further down. 如果您只是想阅读实际问题,请继续阅读下来。
I am fairly new to programming and started going to programming school a few months ago. 我对编程很新,几个月前开始上编程学校。 Lately we have learned to create websites with HTML/CSS and JavaScript and our teacher now wants us to learn how to make desktop apps in JavaScript using Node.js and electron. 最近我们学会了使用HTML / CSS和JavaScript创建网站,我们的老师现在希望我们学习如何使用Node.js和电子在JavaScript中制作桌面应用程序。 He gave us the homework to make a hardware monitor and find modules or frameworks that could help us. 他给了我们做硬件监视器的功课,找到了可以帮助我们的模块或框架。 So far we have only been using standard JavaScript without any frameworks, so this is new to me. 到目前为止,我们只使用没有任何框架的标准JavaScript,所以这对我来说是新的。
Problem: So I found out that Node.js provides the os module which helps you access a lot of hardware and system stuff, but doesn't help you fetch information about your harddrives. 问题:所以我发现Node.js提供了os模块,它可以帮助您访问许多硬件和系统内容,但无法帮助您获取有关硬盘的信息。 Then I Googled and found drivelist: https://www.npmjs.com/package/drivelist 然后我用Google搜索并找到了驱动程序: https ://www.npmjs.com/package/drivelist
I also found a short tutorial on how to install and use it. 我还找到了一个关于如何安装和使用它的简短教程。 I installed it via npm install drivelist
and used sample code from said website, but I get the following message when hovering over const drivelist = require('drivelist');
我通过npm install drivelist
安装它并使用来自所述网站的示例代码,但是当我将const drivelist = require('drivelist');
悬停在const drivelist = require('drivelist');
时,我收到以下消息const drivelist = require('drivelist');
: :
"Could not find a declaration file for module 'drivelist'.
'c:/Users/user.name/Documents/source/Projektarbeit/node_modules/drivelist/lib/drivelist.js'
implicitly has an 'any' type. Try `npm install @types/drivelist` if
it exists or add a new declaration (.d.ts) file containing `declare
module 'drivelist';` [7016]"
I checked out the folders and there is a drivelist.js in the lib folder. 我检查了文件夹,lib文件夹中有一个drivelist.js。 I tried to run npm install @types/drivelist
but it gave me another error: 我试着运行npm install @types/drivelist
但是它给了我另一个错误:
npm ERR! code E404
npm ERR! 404 Not Found: @types/drivelist@latest
And I get the following message when executing my file: 我在执行文件时收到以下消息:
Uncaught Error: A dynamic link library (DLL) initialization routine
failed.
\\?\C:\Users\user.name\Documents\source\Projektarbeit\node_modules\drivelist\build\Release\drivelist.node
at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:166:20)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:740)
at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:166:20)
at Module.load (internal/modules/cjs/loader.js:620)
at tryModuleLoad (internal/modules/cjs/loader.js:559)
at Function.Module._load (internal/modules/cjs/loader.js:551)
at Module.require (internal/modules/cjs/loader.js:658)
at require (internal/modules/cjs/helpers.js:20)
at bindings (C:\Users\user.name\Documents\source\Projektarbeit\node_modules\bindings\bindings.js:84)
at Object.exports.list (C:\Users\user.name\Documents\source\Projektarbeit\node_modules\drivelist\lib\drivelist.js:52)
Here are all my project files and their contents: 这是我的所有项目文件及其内容:
package.json: 的package.json:
{
"name": "projektarbeit",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"drivelist": "^6.4.3"
}
}
main.js: main.js:
const { app, BrowserWindow } = require('electron')
function createWindow () {
win = new BrowserWindow({ width: 800, height: 600 })
win.loadFile('index.html')
}
app.on('ready', createWindow)
index.html: index.html的:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="scripts\index.js"></script>
</head>
<body>
</body>
</html>
index.js: index.js:
const drivelist = require('drivelist');
drivelist.list((error, drives) => {
if (error) {
throw error;
}
drives.forEach((drive) => {
console.log(drive);
});
});
I know this is probably a pretty noob question but I have tried fixing and Googling for an hour now and only found stuff that did not solve my problem. 我知道这可能是一个非常的菜鸟问题,但我已经尝试修复和谷歌搜索一小时了,只找到了解决我问题的东西。
I also don't know what this advice really wants me to do: "add a new declaration (.d.ts) file containing declare module 'drivelist';
[7016]" 我也不知道这个建议真的要我做什么:“添加一个包含declare module 'drivelist';
的新声明(.d.ts)文件declare module 'drivelist';
[7016]”
drivelist
with electron
使用带有electron
drivelist
The native Node modules are supported by Electron, but since Electron is very likely to use a different V8 version from the Node binary installed in your system, you have to manually specify the location of Electron's headers when building native modules. Electron支持本机Node模块,但由于Electron很可能使用与系统中安装的Node二进制文件不同的V8版本,因此在构建本机模块时必须手动指定Electron标头的位置。
... ...
Installing modules and rebuilding for Electron 为Electron安装模块和重建
You can also choose to install modules like other Node projects, and then rebuild the modules for Electron with the
electron-rebuild
package. 您也可以选择像其他Node项目一样安装模块,然后使用electron-rebuild
包electron-rebuild
Electron模块。 This module can get the version of Electron and handle the manual steps of downloading headers and building native modules for your app. 此模块可以获取Electron的版本并处理下载标题和为您的应用构建本机模块的手动步骤。- https://github.com/electron/electron/blob/3-0-x/docs/tutorial/using-native-node-modules.md - https://github.com/electron/electron/blob/3-0-x/docs/tutorial/using-native-node-modules.md
Electron uses it's own instance of Node.js, which is likely not the same version of Node.js that you're using. Electron使用它自己的Node.js实例,它可能与您正在使用的Node.js版本不同。 The drivelist
package you installed was built for the version of Node.js that you're using, and likely won't work with the version of Node.js that Electron is using. 您安装的drivelist
程序包是为您正在使用的Node.js版本构建的,并且可能不适用于Electron正在使用的Node.js版本。
Thankfully there is a handy tool that rebuilds native node modules that are in need of rebuilding for you, and it is conveniently named electron-rebuild
. 值得庆幸的是,有一个方便的工具可以重建需要为您重建的本机节点模块,它可以方便地命名为electron-rebuild
。
electron-rebuild
will exit saying that it was successful while doing nothing at all. 如果你没有, electron-rebuild
将退出,说它是成功的,什么都不做。 Make sure that the electron
package is installed locally in your project (ie without the -g
or --global
flags), not globally otherwise you'll have to pass the global electron version to electron-rebuild
确保electron
包安装在项目的本地(即没有-g
或--global
标志),而不是全局,否则你必须将全球电子版本传递给electron-rebuild
> npm install electron@^3.0.10
Install electron-rebuild
locally 在本地安装electron-rebuild
> npm install electron-rebuild
Install drivelist
locally 在本地安装drivelist
> npm install drivelist
Rebuild the native modules 重建本机模块
> ./node_modules/.bin/electron-rebuild
If you want to incorporate this into the installation of your Electron app, add a postinstall
property to the scripts object in your package.json
file and point it to the electron-rebuild
executable 如果要将其合并到Electron应用程序的安装中,请将postinstall
属性添加到package.json
文件中的scripts对象,并将其指向electron-rebuild
可执行文件
...
"scripts": {
"start": "./node_modules/.bin/electron .",
"postinstall": "./node_modules/.bin/electron-rebuild"
},
...
The revised package.json
file: 修改后的package.json
文件:
{
"name": "projektarbeit",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"start": "./node_modules/.bin/electron .",
"postinstall": "./node_modules/.bin/electron-rebuild"
},
"author": "",
"license": "ISC",
"dependencies": {
"drivelist": "^6.4.3",
"electron": "^3.0.10",
"electron-rebuild": "^1.8.2"
}
}
As a side note, you really should be handling window closure as per the recommendations of the Electron developers: 作为旁注,你真的应该根据Electron开发人员的建议处理窗口关闭:
const { app, BrowserWindow } = require('electron')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({ width: 800, height: 600 })
mainWindow.loadFile('index.html')
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.