What is unsafe-perm flag in npm?

Unsafe-perm flag is set to true to suppress the UID/GID (User Identifier/ Group Identifier) switching when running package scripts.The flag is explicitly set to false to prevent non-root user from installing packages.

If EACCESS error occurs while installing Electron, then running the installation through sudo and the unsafe-perm flag set to true will  install the package.

 

EACCESS error in Electron installation

 

When we try to install Electron using npm in terminal(mac OS), it may throw EACCESS error due to some mismatch in npm permissions. Use sudo to install the package.If error throws up again even after using sudo, then the unsafe-perm flag need to be set to true.

Downgrade npm: command line

Many nodejs apps like createreactnativeapp are designed to work over npm 4 and many of those remain un-ported to latest npm versions.

In such cases we would be needing to downgrade a working npm version that might be higher than the npm versions they work for.

In this respect, createreactnativeapp works only with 4 or lesser and if you have npm version 5 installed or higher, then you can easily downgrade it before using create react app by running:

This will re-install / replace npm with version 4 as the runtime version. So even if for some reason you have to get 5 or latest npm back you can just use:

 

Manage data with multiple child processes in NodeJS

There are some scenarios, where we would like to have multiple system processes spawned as child processes from nodeJs. For Instance,  if we wanted to do a batch download of resources from some other server etc.

The gist link contains one such example. The simplest way to identify and get result back from each individual spawned processes is to create the task itself as separate class / function enclosure. So we call all these tasks in their context and perhaps add in a callback with specific identifier associated with each spawned task.

const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();

view raw
manage-multitask.js
hosted with ❤ by GitHub

const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();

view raw
manage-multitask.js
hosted with ❤ by GitHub

const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();

view raw
manage-multitask.js
hosted with ❤ by GitHub

const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();

view raw
manage-multitask.js
hosted with ❤ by GitHub

Using env variables in NodeJS

You can set and use environment variables within NodeJS processes as below:

Windows:

Unix:

and inside your app javascript you can check across this as