Categories
JavaScript Answers

How to write to stdin from an already initialized process with Nodejs Child Process?

to write to stdin from an already initialized process with Nodejs Child Process, we call trhe stdin.write method.

For instance, we write

const spawn = require("child_process").spawn;
const child = spawn("phantomjs");

child.stdin.setEncoding("utf-8");
child.stdout.pipe(process.stdout);

child.stdin.write("console.log('Hello from PhantomJS')\n");

child.stdin.end();

to call stdin.write to write to stdin.

And then we stop writing with stdin.end.

Categories
JavaScript Answers

How to import from root with JavaScript?

To import from root with JavaScript, we use ~.

For instance, we write

import 'foo' from '~/components/foo.js';

to use ~ as the alias for the root folder when using import to import the foo module.

Categories
JavaScript Answers

How to create an ISO date object in JavaScript?

To create an ISO date object in JavaScript, we call the toISOString method.

For instance, we write

const isoDate = new Date().toISOString();

to create a date with the Date constructor.

And then we call toISOString to return the ISO string version of the date and time.

Categories
JavaScript Answers

How to get the npm global path prefix with Node?

To get the npm global path prefix with Node, we run npm config.

To do this, we run

npm config get prefix

to return the global package path prefix.

Categories
JavaScript Answers

How to remove everything after last backslash with JavaScript?

To remove everything after last backslash with JavaScript, we call the substr and lastIndexOf methods.

For instance, we write

const t = "\\some\\route\\here";
const newT = t.substr(0, t.lastIndexOf("\\"));

to call lastIndexOf to return the last index of '\\'.

Then we call substr to return the substring in t before index 0 and the index returned by lastIndexOf.