Node webkit application with USB module failing - javascript

I am writing an desktop application using node-webkit. I have written script using Node USB module which is working fine if i run that script using node. It lists all usb devices , connects,reads,writes. Now I want to achieve same using node-webkit application where HTML has a button on which we have to do these things. Now for this we require to configure node modules. I have added that module to my project and then did process as mentioned
package.json:
{
"name": "node-webkit-angular-bootstrap-starter",
"version": "0.0.1",
"description": "Starter destkop app using node-webkit, html5, angular, and bootstrap",
"main": "app/index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"window": {
"title": "node-webkit-angular-bootstrap-starter",
"toolbar": true,
"frame": true,
"width": 1024,
"height": 768,
"position": "center"
},
"dependencies" : {
"node-pre-gyp": "0.6.9"
},
"bundledDependencies":["node-pre-gyp"],
"devDependencies": {
"aws-sdk": "~2.0.0-rc.15"
},
"scripts": {
"install": "node-pre-gyp install --fallback-to-build"
},
"binary": {
"module_name": "usb",
"module_path": "./app/lib/binding/",
"host": "https://github.com/camsoupa/node-usb"
}
}
Home.js:
(function () { 'use strict'; app.controller('homeController', function ($scope) {alert("Heelooooo");
var gui = require('nw.gui');
var os = require('os');
$scope.settings = [];
$scope.usb = require('usb'); });})();
I am getting following error for running the application using nw
"%1 is not a valid Win32 application.
↵D:\Projects\…er\node_modules\usb\src\binding\usb_bindings.node"}
Can some one please help me?

I solved this issue in my case by compiling for the correct architecture. In my environment, node is 32-bit while nw is 64-bit. Changing directories to the usb module, the following two commands resolved the issue by creating a 64-bit version of the native bindings:
nw-gyp configure --target=0.12.3 --module_name=usb_bindings --module_path=..\src\binding\ --target_arch=x64
nw-gyp build --target=0.12.3 --module_name=usb_bindings --module_path=..\src\binding\ --target_arch=x64
The module_name and module_path variables are necessary because for whatever reason, nw-gyp isn't getting their values from the binding.gyp file.
One more thing that can cause the error you described is renaming nw.exe to something else. As long as it is named nw.exe, the symbols will bind correctly.

Related

npm pack removing file extensions from import

I'm trying to a create an internal library for work.
All seems to be fine until I attempt to use in another project.
The file extension in all of the import statements seems to have been removed during the npm pack phase.
In other words, the statement:
import * as Account from './modules/account.js'
becomes:
import * as Account from './modules/account'
This causes the import to fail.
I originally thought this may have been because I used the .js extension instead of .mjs, but switching to .mjs yields the same results.
main.js
import * as Account from './modules/account.js'
Account.secretSquirrel().then( data => console.log( 'inspector gadget', data ) );
node version
v16.15.0
package.json (sensitive info redacted)
{
"name": "#Nunya",
"version": "0.0.0",
"description": "Nunya",
"private": true,
"main": "./lib/main.js",
"scripts": {
"build": "npm run pack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "Nunya.git"
},
"author": "Nunya",
"license": "ISC",
"type": "module",
"exports": {
".": {
"require": "./lib/main.js",
"default": "./lib/main.js"
},
"./Account": "./lib/modules/account.js"
}
}
As far as I can tell, this shouldn't be happening. Not sure how to resolve
Evidently, adding an import object defining the resolutions for the relative paths to package.json resolves this issue. Although, I'm certain there's a non-manual way to accomplish this, it is unbeknownst to me at this time.
If anyone knows, please let me know

Electron - preload.js is not loaded and error is occurred on Windows 10

When I built npm start the app on Windows 10, it does not work normally which worked fine on macOS.
package.json
{
"name": "SimpleTimer",
"version": "1.0.0",
"productName": "SimpleTimer",
"copyright": "",
"description": "",
"main": "main.js",
"scripts": {
"start": "node_modules/.bin/electron .",
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"electron": "^10.1.2"
}
}
I got the following error message.
TypeError: ipcMain.handle is not a function where this applies:
main.js
ipcMain.handle("ipc-timer-start", () => {
if ( isWorking === true ) {
return true
}
else {
StartTimer();
isWorking = true;
}
return true;
});
This function is the recipient of the ipc communication from ipcRenderer.invoke() written in preload.js. That's the invoke() method in TimerStart api in the following source.
preload.js
const { contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld(
"api", {
TimerStart: () =>
ipcRenderer.invoke("ipc-timer-start") /*** HERE ***/
.then(result => result)
.catch(err => console.log(err)),
TimerStop: () => ipcRenderer.send("ipc-timer-stop"),
TimerReset: () => ipcRenderer.send("ipc-timer-reset"),
DisplayTimer: (channel, listener) => {
ipcRenderer.on("ipc-display-timer", (event, arg) => listener(arg));
}
}
);
Of course, preload.js is specified when it created the BrowserWindow.
main.js
mainWindow = new BrowserWindow({
title: config.name,
width: 1024,
height: 640,
minWidth: 1024,
minHeight: 640,
webPreferences: {
worldSafeExecuteJavaScript: true,
nodeIntegration: false,
contextIsolation: true,
preload: __dirname + '/preload.js' /*** HERE ***/
}
});
After skipping this error message dialog, I checked with the DevTools and it looks like preload.js is not loaded from the statement of Unable to load preload script:.
Given these statements, it seems that preload.js is not properly included in the build, what should I do?
Once again, it worked fine on macOS, but it doesn't work properly on Windows 10 due to these issues.
I was able to solve this problem oneself.
This problem wasn't in electron, it was in my Parallels Desktop for Mac environment.
I was trying to get the Electron working directory to work on Windows 10 through the Parallels network drive, but it doesn't seem to recognize the build file path properly.
Currently, even if you have a local install of Electron instead of a global install, it doesn't seem to be able to build properly.
npm install electron#latest --save-dev
It seems that a project that could be built on a macOS cannot be built directly on Windows 10.
Electron is "electron ." command to build on Windows 10, it seems to extract the necessary files on the C:\Users\[UserName]\AppData\Roaming\npm\ path. I don't know the exact reason, but it seems to be inconsistent with locally installed Electron.
If you want to run your project on Windows 10, copy the project directory and input "npm install" command to reinstall Node modules such as Electron. It will automatically reinstall the dependent packages listed in package.json.
As a result, I can finally build "node_modules/.bin/electron ." to build it without any problems.

Globally-installed NodeJS npm module does not execute the main/bin JavaScript file with node

I've a package.json like this:
{
"name": "some-module",
"version": "1.0.0",
"bin": "./bin/some-module.js",
"main": "./bin/some-module.js",
"description": "Some module description",
"homepage": "http://my.home.page.com",
"author": {
"name": "Matias Fidemraizer",
"email": "no-email#no-email.com",
"url": "http://some.url.com"
},
"engines": {
"node": ">=0.4.0"
},
"keywords": [
"somekeyword"
],
"license": {
"type": "Apache v2",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"preferGlobal": true,
"repository": {
"type": "git",
"url": "git://github.com/some/repo"
},
"dependencies": {
"somedependency": "*"
}
}
When I try to install the whole module typing npm -g install /path/to/module/folder, npm creates a .cmd file on AppData folder in the default node_modules location for global installations as expected.
But generated code doesn't include node.exe or node:
"%~dp0\node_modules\some-module\bin\some-module.js" %*
... so when I try to execute my some-module module in CMD, PowerShell or whatever, it's executed using Windows Script Host (WSH).
For that reason I thought comparing package.json of some existing module like YUIDocJS would be enough to find out what's causing this problem but I can't figure out what's wrong in my own package.json so it doesn't create the expected global installation.
Thank you in advance for your effort.
Do you have the shebang #!/usr/bin/env node at the top of the file referenced in the bin property of your package.json? Even though the shebang is a *nix specific directive, npm depends on its presence to create the shim for the .cmd

node_modules is not recognized as an internal or external command

I'm trying to write a test automation script using appium, jasmine, and perfecto mobile. I'm using the project cloned from the following URL with my own configuration Appium Javascript Example
The problem is when I execute the npm test command I get the following error
node_modules is not recognized as an internal or external command
This is how the packages.json script looks like:
{
"name": "perfecto_appium_sample",
"version": "1.0.0",
"description": "The following sample shows how to Install an application and use WebDriverIO to automate and test it.<br/> It uses selendroid test application which can be downloaded from [here](https://github.com/PerfectoCode/AppsForSamples/tree/master/selendroid-test-app-0.17.0).",
"main": "perfectoSpec.js",
"scripts": {
"test": "node_modules/webdriverio/bin/wdio wdio.conf.js",
"start": "wdio wdio.conf.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"wdio": "^0.3.3",
"wdio-jasmine-framework": "^0.2.19",
"wdio-mocha-framework": "^0.5.12"
},
"dependencies": {
"wd": "^1.5.0",
"webdriverio": "^4.10.2"
},
"keywords": []
}
you need to provide relative path properly:
"scripts": {
"test": "node ./node_modules/webdriverio/bin/wdio wdio.conf.js",
"start": "wdio wdio.conf.js"
}
Just remove the paths "node_modules/webdriverio/bin/" and simply specify "wdio wdio.conf.js". It should work.

Node.js nwjs package compilation error: Invalid file descriptor to ICU data

I'm trying to create my first package with nwjs, so I created a simple app with only nwjs module installed locally with npm. My app main file has only a console.log("Hello World!"). I zipped the app files - keeping them in the root - and renamed to app.nw. I basically followed this doc. The package tree is the following:
app.nw
- node_modules
- nw (and all its files inside)
- package.json
- app.js
And my package.json is:
{
"name": "app3",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"window": {
// Some properties here
}
}
Then I created an empty folder and copied app.nw to it. From the module folder /nw/nwjs I copied the files: nw.exe, nw.dll, nw_elf.dll and ffmpeg.dll to it, and dragged app.nw over nw.exe. The debug.log file shows me the following error:
[0524/110408:ERROR:icu_util.cc(157)] Invalid file descriptor to ICU data received.
[0524/110408:FATAL:icu_util.cc(260)] Check failed: result.
I can't find anything related to this error and this tool over the web. I'm running it in a Win7 x64. I also tryied downloading nwjs manually from github and tryied with x64 and x86 binaries, but they all give me the same error.
Below, the backtrace if needed:
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1B7AB1+225089]
std::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >::operator= [0x000007FEDA1640BC+259612]
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1F2E58+467688]
v8::Extension::auto_enable [0x000007FEDA1203D1+368897]
v8::Extension::auto_enable [0x000007FEDA11ED19+363081]
ChromeMain [0x000007FED9CE0EB5+133]
GetUploadedReportsImpl [0x000000013F201251+3297]
GetUploadedReportsImpl [0x000000013F20097F+1039]
IsSandboxedProcess [0x000000013F264628+231640]
BaseThreadInitThunk [0x0000000076C359BD+13]
RtlUserThreadStart [0x0000000076E6A2E1+33]
What am I missing here?
I got it to work with the following package.json:
{
"name": "app4",
"version": "1.0.0",
"description": "",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"single-instance": true,
"package": ".json",
"window": {
"title": "app4",
"toolbar": false
}
}
I'm actually not sure what specific property which I removed/added to made it to work. Then I zipped it to package.nw and put in the same folder as nw.exe to run it.

Categories

Resources