UAC management for an app build with electron-packager - node.js

I used the winreg npm package for my app and I need elevated privileges for the uses I have.
I would like to associate a manifest file with an app build with electron (atom-shell) and packaged on windows with electron-packager but I can not find any way to do it easily.
An old app of mine was made with cx_freeze and it sufficed to put the file with the name app.exe.manifest in the same directory.
I can't understand why it doesn't work this time.

I solved this problem.
So first thing to know is that an external manifest won't count if an embedded manifest is already present. (see answer to https://superuser.com/questions/905103/what-does-it-mean-to-enable-windows-to-prioritise-external-manifests)
And it turns out that the electron.exe has already a manifest embedded
and the package done with electron-package is mostly a rename of the
electron executable.
So in order to embed a news manifest with the right privileges needed, I took the manifest in the electron repo (see https://github.com/atom/electron/blob/master/atom/browser/resources/win/atom.manifest), edited the security section and used the mt.exe tool to add the new manifest (see https://msdn.microsoft.com/en-us/library/aa375649%28v=vs.85%29.aspx)
And tadaa !

Related

Is it possible to install and use node modules in my Chrome Extension? [duplicate]

I tried so but I have a 'require is not defined' error. I can't find information about that, can someone enlighten the noob in me please?
It's possible, but you have to be careful. Trying to require() a package means that node will try to locate its files in your file system. A chrome extension only has access to the files you declare in the manifest, not your filesystem.
To get around this, use a module bundler like Webpack, which will generate a single javascript file containing all code for all packages included through require(). You will have to generate a separate module for each component of your chrome extension (e.g. one for the background page, one for content scripts, one for the popup) and declare each generated module in your manifest.
To avoid trying to setup your build system to make using require() possible, I suggest starting with a boilerplate project. You can check out my extension to see how I do it.
An updated answer for 2022
Short answer: yes, you can require/import packages. Rather than going through the tedious work of setting up & configuring a bundler like Webpack on your own (especially if you have no experience with them), there are now build tools you can use to create the boilerplate "scaffolding" for a Chrome extension:
Extension CLI -- this one is well-documented and you can also reference the source code of some Chrome extensions that have used this tool (READ: learn how others have set up their code).
Chrome Extension CLI
Benefits of using them:
New projects are initiated with a default project file structure. Super helpful.
They support modern Javascript (ES6, ES2021), so modules work fine.
They already have bundlers integrated and pre-configured (Webpack in both above cases I think). You therefore don't need to install and configure any on your own.
You can use npm as normal to install any packages/dependencies you need.
Then of course, let the official documentation for Chrome Extensions guide you through the rest.
It's not possible to require node modules directly within a chrome extension. However, it is possible to bundle node applications and packages into the browser for use with your extensions. See here for more: Is it possible to develop Google Chrome extensions using node.js?
Yes, It is possible with esm npm packages.
require is commonjs module loader.
Browser doesn't support commonjs modules system
so that this error showed.
Method 1:
Run npm init -y and add "type" :"module" in your package.json.
create path.js file
add this line in path.js
const fullPath = await import.meta.resolve("npm-pkg-name");
const path = fullPath?.match(/(/node_modules.*)/)[0];
console.log(path);
add this line inside package.json
"path": "node --experimental-import-meta-resolve path.js",
Copy console output text. Replace package name with this copied path.
Method 2:
Install other npm package to find and replace
npm packages' virtual path to real path so that chrome browser will find it.
Install Path-fixxer
Add this line in path.js
import setAllPkgPath from "path-fixxer";
setAllPkgPath();
then run command : npm run path.
Now open browser to test it.

How to download npm packages instead of using the command line

I'm working in a completely offline environment & lately, I've had to code some react applications. I wonder if there is a way to download npmjs packages manually without using the command npm install <package-name>.
For example, while I'm coding with python in the same offline env, I'm downloading everything from PyPi manually & use the .whl in my offline environment using removable devices from one online environment to the offline environment.
I'll appreciate for any help or direction to the solution.
You need to download their source from the github. Find the main file and then include it in your main file.
You need to find the source and go through the package.json file. There you can find which is the main file. So that you can include that in your application.
To include example.js in your app. Copy it into your application folder.
Once you do that, your requirements will just be:
var moduleName = require("path/to/example.js")
It will always look for a node_modules directory at the root of your app (and a few other places as well).
It's important you download the full repo and not just the lib folder if you plan to use it this way since the package.json file is used to find the main entry point. As long as the repository has a valid package.json file it should work.[

How to make a Nodejs distributable command line application

I'm making a command line application with commander, inquirer and nightwatch as top dependencies. The main purpose of the app is for automation and testing.. Is there any way i can make this distributable instead of publishing it as npm package. I want the same functionality as those cli made with python, where it can be setup, and run on a target machine. is this possible? Thank you
Here are two open source projects (PKG and Nexe) that enable you to package your Node.js project into an executable:
https://github.com/zeit/pkg/blob/master/README.md
Or
https://github.com/nexe/nexe/blob/dev/README.md
You can use either one to make an executable of your project.

Is it possible to require npm modules in a chrome extension ?

I tried so but I have a 'require is not defined' error. I can't find information about that, can someone enlighten the noob in me please?
It's possible, but you have to be careful. Trying to require() a package means that node will try to locate its files in your file system. A chrome extension only has access to the files you declare in the manifest, not your filesystem.
To get around this, use a module bundler like Webpack, which will generate a single javascript file containing all code for all packages included through require(). You will have to generate a separate module for each component of your chrome extension (e.g. one for the background page, one for content scripts, one for the popup) and declare each generated module in your manifest.
To avoid trying to setup your build system to make using require() possible, I suggest starting with a boilerplate project. You can check out my extension to see how I do it.
An updated answer for 2022
Short answer: yes, you can require/import packages. Rather than going through the tedious work of setting up & configuring a bundler like Webpack on your own (especially if you have no experience with them), there are now build tools you can use to create the boilerplate "scaffolding" for a Chrome extension:
Extension CLI -- this one is well-documented and you can also reference the source code of some Chrome extensions that have used this tool (READ: learn how others have set up their code).
Chrome Extension CLI
Benefits of using them:
New projects are initiated with a default project file structure. Super helpful.
They support modern Javascript (ES6, ES2021), so modules work fine.
They already have bundlers integrated and pre-configured (Webpack in both above cases I think). You therefore don't need to install and configure any on your own.
You can use npm as normal to install any packages/dependencies you need.
Then of course, let the official documentation for Chrome Extensions guide you through the rest.
It's not possible to require node modules directly within a chrome extension. However, it is possible to bundle node applications and packages into the browser for use with your extensions. See here for more: Is it possible to develop Google Chrome extensions using node.js?
Yes, It is possible with esm npm packages.
require is commonjs module loader.
Browser doesn't support commonjs modules system
so that this error showed.
Method 1:
Run npm init -y and add "type" :"module" in your package.json.
create path.js file
add this line in path.js
const fullPath = await import.meta.resolve("npm-pkg-name");
const path = fullPath?.match(/(/node_modules.*)/)[0];
console.log(path);
add this line inside package.json
"path": "node --experimental-import-meta-resolve path.js",
Copy console output text. Replace package name with this copied path.
Method 2:
Install other npm package to find and replace
npm packages' virtual path to real path so that chrome browser will find it.
Install Path-fixxer
Add this line in path.js
import setAllPkgPath from "path-fixxer";
setAllPkgPath();
then run command : npm run path.
Now open browser to test it.

cloud_package.cspkg file was not created after installing "socket.io-servicebus"

I'd like to use the "socket.io-servicebus" module to my node.js application.
But I encountered that a problem.
After installing the "socket.io-servicebus", cloud_package.cspkg file was not created by "Publish-AzureServiceProject" command.
I'm using "Windows Azure PowerShell" on Windows7 64bit edition.
Here is the procedure.
New-AzureServiceProject test1
Add-AzureNodeWebRole www
cd www
npm install socket.io-servicebus
Publish-AzureServiceProject -ServiceName xxx ...
[ cloud_package.cspkg will not created ]
By the way "Start-AzureEmulator -Launch" will be succeeded and we can test own application.
Please give me some advices. thank you.
It looks like the issue here is due to a known path length limitation. Azure has a limitation on paths in the package being more than 255 chars and in this case bringing in socket.io WITH all of it's dependencies is hitting that path.
There are several possible work arounds here.
A. - Zip up node modules and extract on the server.
Basically you zip up your modules and publish the module zip within the package. Then you can use an Azure startup task (in your cscfg) on the server to unzip the files.
Publish-AzureServicePackage will grab anything in the project, so in this case you just have a little script that you run before publishing which creates the node_modules archive and deletes node_modules.
I am planning to do a blog post on this, but it is actually relatively easy to do.
B. - Download node modules dynamically
You can download modules in the cloud. This can also be done with a startup task as is shown here.
If you look in that post you will see how you can author a startup task if you decide to do the archive route.
Feel free to ping me with any questions.

Resources