"..." in every action in node.js - javascript

First I want to clarify that I'm new in node.js and I'm not a linux user, so the cmd isn't my thing.
Every time I try to do something, for example
sudo npm install express#2.6.5 (as it's in a sitepoint ebook example)
or
node example.js (as another tutorial)
Every time, the response is a "..." in the screen (http://www.screencast.com/t/dx6VZhsVtl), so my 3 questions:
Why this happens (I'm missing some configuration or something)?
How to install packages (as the ... is always the response)
When I run a program, I don't have to specify the absolute path, just the file?

It looks like you're trying to run the file example.js, but you're still running a node REPL session (indicated by the >, see the link for more details). Press Control+C a few times to exit to get back to regular terminal (it should show something like username#machineName:).
To run a node file, use node [filename].
To open a node REPL session, simply use node.

Related

How do I reactivate terminal in VS Code?

I am very new to coding and following a tutorial at the moment (building a simple CRUD app). The issue is that once I run the command "nodemon app", I can no longer use any other commands. I can type it out but hitting enter does nothing. How do I use other commands without opening a new terminal?
click here for image
Ctrl+C will interrupt the running process in a terminal, in this case, killing nodemon.
That would allow you to run other commands, but will lose you your nodemon process.
Another option (that might be more suitable for VSCode) is to open a second terminal (Ctrl+Shift+`, or Ctrl+Shift+5 to open side-by-side). In the second terminal, you can then execute whatever other commands you want.
Alternatively, you can append the & character to a command to have it run in the background. This would allow you to run something like nodemon app & and then run other commands, but be aware that in this arrangement, the output of the commands will become interleaved - so if the node application is writing to the console and the other command also outputs information, they might become tricky to read.

Changes to minio browser do not appear when running minio server

My goal is to modify the Minio browser for front end appearance in house. I'd like to add features too but can't seem to get either to work and feel like I'm missing something about how go accesses npm or the browser.
I have made changes to the Minio web browser (javascript) and can see them when running with npm (in ./browser 'npm run release;npm run dev'), but when I try to run minio server built with the same git clone (changes is browser subdir) and browse to localhost:9000 I don't see any of the changes.
It would also be nice to run the browser with npm and connect to the running server "./minio server ~/data", but they don't seem to talk and I'm unclear on how they're connected.
This seems to be a simple case of all the things I tried and in the right order.
Correct order:
cd browser; npm run release
cd ..; make
./minio server ~/minio-data
It seems I'd tried all of these separately but not in the obvious order. I'm assuming npm makes the ui-assets.go which gets included by the make

Run code once for a single version of a Node.js based CLI

I have written a CLI in Node.js, that you can globally install using npm. Now I want to run a specific piece of code the first time a user runs my CLI. But it should only run once.
My question is: How could I detect that the CLI is run for the very first time?
Of course, I could write a file into the user's home directory, and if it exists, skip code execution. This would be pretty simple.
But now things get slightly more complicated: I want this check to be re-run when the user updates the CLI to a new version. So, again, I could write a file into the user's home directory, and store the versions in it, for which I have run the "once"-code block.
But this again means that every time the user runs the CLI it has to open the file, parse it, look for the version, and so on. I fear that this could negatively impact startup performance.
Is there a better way to solve this?
I have a stupid idea, treat this as an academic example of hacky metaprogramming. Create a script named script.js:
var fs = require('fs');
if(!process.env.firstRun){
var content = fs.readFileSync("script.js", "utf8");
fs.writeFileSync('script.js', 'process.env.firstRun=true;\r\n' + content, 'utf8');
console.log('first run');
} else {
console.log('next run');
}
The script simply overwrites itself by adding an additional flag declaration at the beginning if it's not set. Otherwise it goes the other path:
λ node .\script.js
first run
λ node .\script.js
next run
λ node .\script.js
next run
The above was just for fun. For production code you should go for a configuration file as you proposed. Reading single small file is not a big deal, according to this method it takes <0.5ms in my setup (I5 processor, SSD drive), so it's definitely a way to go.

Run Windows Node.js script in background when the user logins by setting a Run key

I'm running on Windows Server 2012R2.
I have a node.js script which I want to run when the user performs logins.
To do this I'm setting the command to run in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry path.
I set this command:
/path/to/node/node /path/to/node/script args
This works fine, but it will spawn a terminal showing the command output, while I'd like to run this command in background and detached from any command terminal.
I tried to replace the previous command with:
start /b /path/to/node/node /path/to/node/script args
but in this case there is no evidence that the script was even started.
I also tried to wrap the following command in a .bat script:
start \b node script args
set the name of the script in the Run key: in this case I can see terminal flash but then the script is not running anymore (I suppose that the script is executed but then it is stopped as soon as the parent process is terminated).
I want to avoid to convert the script in a windows service, as long as a simpler solution is possible.
A solution not using the Run registry key is also fine, as long as it fulfils my requirement (run a script in background when the user logins).
Actually it's pretty easy , use forever.js module
after installing the module use
"forever start main.js"
It will start running as your background process

How to add tab completion to a Nodejs CLI app

I want to add tab completion to a Nodejs CLI app (And preferably generate the tab completion dynamically).
I found a few npm modules but not sure how to really implement them:
https://github.com/hij1nx/complete
https://github.com/mklabs/node-tabtab
So what I am looking for is so I can have a nodejs file that is something like:
my-cmd create arg1 arg2
But then I might want to autocomplete like:
my-cmd cr<tab> -> create
Thanks!
Use omelette package that I built. If you have any questions, please contact me.
Edit - fast answer
After I answered, I kept reading tabtab source a bit and noticed that I can also run
pkgname completion install
to install the completion. since my environment was already dirty, I don't know if it actually did anything, but seems to me like it did..
Longer answer
#CameronLittle has given great documentation.
For the impatient, you can start by running
sudo bash -c 'pkgname completion > /etc/bash_completion.d/pkgname'
source /etc/bash_completion.d/pkgname
This will add completion to your current bash session.
As far as I know, new sessions will get the completion automatically.
To make the process seamless for user, you can use the install and postinstall hooks in package.json
https://docs.npmjs.com/misc/scripts
Make sure to not print anything by default. means running pkgname should result in no output, or otherwise it will not work.
important! install tabtab only from master
It seems tabtab has an annoying bug that was resolved in master but never got into a release..
The relevant commit to fix it is this:
https://github.com/mklabs/node-tabtab/commit/f8473555bf7278a300eae31cbe3377421e2eeb26
which handles completion for strings starting with --.
The commit if from february 2014, however the latest release as of (Jan. 2015) is 0.0.2 from Jan. 2014.. I assume there will not be more releases.
So if you want to get this fix, and you should(!), install tabtab only from master.
don't waste 2 hours figuring out what you did wrong like me :)
How did i reach this answer? TL;DR
While #CameronLittle's answer gives the explanation behind the scene, I would like to explain how to I reached the answer.
I tried using the package tabtab which has an explicit section about installing it. see https://www.npmjs.com/package/tabtab#completion-install
However, that didn't seem to work for me.
Looking at the code they instruct to add, I see the following process.argv.slice(2)[0] === 'completion' which made me run the command pkgname completion, which outputs something that starts with
###-begin-pkgname-completion-###
### credits to npm, this file is coming directly from isaacs/npm repo
#
# Just testing for now. (trying to learn this cool stuff)
#
# npm command completion script
#
# Installation: pkgname completion >> ~/.bashrc (or ~/.zshrc)
#
the words this file is coming directly from isaacs/npm repo made me wonder more. following the other answer here, I looked at /etc/bash_completion.d/npm - which showed the same exact content.. and so the comment.
I decided to run
pkgname completion > /etc/bash_completion.d/pkgname
however that requires sudo permissions and so becomes
sudo bash -c "pkgname completion > /etc/bash_completion.d/pkgname
and then, in order to apply it to current bash session I had to run
source /etc/bash_completion.d/pkgname
and voila! it works!
when I tried to open another terminal, it still worked, so I assume it will apply to all users. if not - you should add it to .bashrc or something..
I would just like to add that there is a
npm package yargs that enables bash-completion shortcuts for commands and options.
It has the option to output a .bashrc completion script. Bash completions are then enabled by sourcing the generated script.
It is currently an actively maintained package on npm with over a million downloads a month.

Categories

Resources