stinsfire
stinsfire

Reputation: 45

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. 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.

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. Then I Googled and found drivelist: 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');:

"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. I tried to run npm install @types/drivelist but it gave me another error:

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:

{
  "name": "projektarbeit",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "drivelist": "^6.4.3"
  }
}

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:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <script src="scripts\index.js"></script>
  </head>
  <body>

  </body>
</html>

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]"

Upvotes: 4

Views: 1653

Answers (1)

user4639281
user4639281

Reputation:

Using drivelist with electron

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.

...

Installing modules and rebuilding for Electron

You can also choose to install modules like other Node projects, and then rebuild the modules for Electron with the electron-rebuild package. This module can get the version of Electron and handle the manual steps of downloading headers and building native modules for your app.

- 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. 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.

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.

  1. Make sure you installed BuildTools when installing Node.js. If you didn't, electron-rebuild will exit saying that it was successful while doing nothing at all.

  2. Make sure that the electron package is installed locally in your project (i.e. without the -g or --global flags), not globally otherwise you'll have to pass the global electron version to electron-rebuild

     > npm install electron@^3.0.10
    
  3. Install electron-rebuild locally

     > npm install electron-rebuild
    
  4. Install drivelist locally

     > npm install drivelist
    
  5. 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

...
"scripts": {
    "start": "./node_modules/.bin/electron .",
    "postinstall": "./node_modules/.bin/electron-rebuild"
},
...

The revised package.json file:

{
  "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:

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()
  }
})

Upvotes: 3

Related Questions