create a bat file to run node commands - node.js

I'm trying to run node commands like npm install and node server.js using a bat file.
I'm planning to put the bat file in the application root folder.
So, the steps I want my bat file to do are:
Open command prompt in the same folder.
Run npm install
Run node server.js

You can create a .bat file and you can execute it with the following code
const { exec } = require('child_process');
exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
EDIT
You just create my.bat and add the following commands
cd "Your project path"
npm install
node server.js

Related

Run node_module package command in js file

In the package.json file, I have a line in the scripts object like this:
"scripts": {
// some commands..
"runpackage": "someNpmPackage -args"
}
This someNpmPackage is a package I have in my node_modules, but not in the command line. (i.e. I can run the command in the terminal).
This works fine. However, I want to be able to do in the scripts: "runPackage": "node scripts/runPackage.js"
I try something like
var exec = require('child_process').exec;
exec('someNpmPackage -args', function (err) {
if (err) {
console.log(err.message);
process.exit();
}
console.log('success');
});
But all I get is, /bin/sh: someNpmPackage: command not found.
How can I make the exec function know about this package?
Why don't you install the someNpmPackage globally in your system?
Try in the root project folder, on the command line: ./node_module/.bin/someNpmPackage. If this command works, then it should work with exec.

How to execute a mongoDB shell script via Node.js?

I am working on my class project in which I want to demonstrate the use of mongoDB sharding. I am using mongoDB node.js native driver. I got to know there is no sharding functionality in this driver. So, I have to write shell script to do sharding. So, Is it possible to do this somehow like this:
node myfile.js (executes my shell script and run my code)
Given that you already have a shell script, why not execute that through the Child Process module. Just use the below function to run the script that you have.
child_process.execFileSync(file[, args][, options])
Note that the script should have run permissions(use chmod a+x script otherwise)
why don't you consider using npm run scripts?
if you want the script to run standalone, add scripts with test/start or both to your package json,
"scripts": {
"test": "node mytestfile.js",
"start": "node ./myfile --param1 --param2"
},
and run npm run test or npm run start which can execute the script file. this way you can even pass parameters to the script.
or the elegant child_process way,
const { exec } = require("child_process");
exec("node myfile.js", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
stderr and stdout will show the progress of the script as you build further.
hope this helps.

Passing an argument to npm run script

I am creating a script in npm package.json.
The script will run yeoman to scaffold my template and then I want to run a gulp task to do some more stuff to a specific file (inject using gulp-inject)
The npm task looks like this:
"scaffolt": "scaffolt -g scaffolt/generators template && gulp inject"
Now, i need to be able to call the command from the command line giving a name to my template.
The command I need to run is the following:
npm run scaffolt {templateName}
but if I do this, then I try to run a gulp task called the same as the typed {templateName}.
A quick example: If I run npm run scaffolt myTemplate then the second part of this will try to run a task called gulp myTemplate, failing.
Is there any way to pass the {myTemplate} name as an argument to the second part of the script so that it can be used in the gulptask?
The gulp task currently only console.log the process.argv.
You can pass arguments to the npm run-script. Here is the documentation.
Make gulp tasks for these operations.
//gulpfile.js
const gulp = require('gulp');
const commandLineArgs = require('command-line-args');
const spawn = require('child_process').spawn;
gulp.task('inject', ['scaffolt'], () => {
console.log('scaffolt complete!');
});
gulp.task('scaffolt', (cb) => {
const options = commandLineArgs([{ name: 'templateName' }]);
//use scaffolt.cmd on Windows!
spawn('scaffolt', ['-g', 'scaffolt/generators', options.templateName])
.on('close', cb);
});
And in your package
//package.json
"scripts": {
"scaffolt": "gulp inject "
}
And to run it npm run scaffolt -- --templateName=something
Tip: npm run-script appends node_modules/.bin directory in the PATH so we can spawn executables just like they are on the same folder!
You can use magical $npm_config_<exampleVarName> in script definition and then pass the value of it either from env variable named match exampleVarName or pass it in command line you add --exampleVarName=ValueHere
in your case
//package.json
"scripts": {
"scaffolt": "scaffolt -g scaffolt/generators $npm_config_templateName && gulp inject"
}
then run it as
npm run scaffolt --templateName=whatever

NPM - Error: Cannot find module '../'

I'm reading this doc on hapi-auth-cookie and trying to run sample server.here is what i did :
1-putting sample server in server.js
2-npm init
3-node server.js
4-npm install --save hapi
5-node server.js but this time i get a new error
Error: Cannot find module '../'
somewhere in the code it's requiring '../'
server.register(require('../'), (err) => {
if (err) {
throw err;
}
But i don't understand this part really.you can see the full code in the link above.what should i do? thanks
I had the same error. I just deleted the node_modules directory and rerun install.
rm -rf node_modules/
npm install
After that the app runs correct again.
Maybe there isn't a index.js file in directory ../.
Looking at the link you posted, the index.js file of that module is in the /lib (https://github.com/hapijs/hapi-auth-cookie/tree/master/lib) directory and usually you want to import a module by name in the npm construct.
So put your server.js in /lib
You can replace
server.register(require('../'), (err) => {
with
server.register(require('hapi-auth-cookie'), (err) => {
and be sure to run
npm i -S hapi-auth-cookie
and
npm i -S hapi
before you start your server

executing node command from js file

I'm neewbe in writing node.js command line scripts. In my project we have a multi-app environment. To setup a dev env we need to launch 2 or more apps, so I'm wondering if it's possible to write a script which will do the following :
run a webpack --watch for my common project which will generate a bundle files
propagate bundles to the other apps.
launch webpack-dev-servers for others apps in separate terminals.
Is it possible to do that from a node script? At this moment I only achieved to implement the second step - a file propagation.
You can spawn a child process with node easily. So you could start multiple child processes from one file.
const exec = require('child_process').exec;
const child = exec('webpack --watch',
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
Check the corresponding docs: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Resources