Best practice for distributing a nodejs command line application - node.js

How do people expect a nodejs commandline application to be organised when you distributed it via npm?
Would they expect to have to build it? Install it locally or globally? Should it always output a bin.js? Do you need some kind of alias / script to run it (via bin.js?).

Use the bin field in package.json to specify the entrypoint javascript file for your CLI.
Add #!/usr/bin/env node to the top of your entrypoint js file.
Specify the files which need to be packaged with your app, using the files field of package.json
In README, provide an npm install command line which a consumer can use to install your app, for example it could point at a github repository, or a package uploaded to the npm registry, which you can create with npm pack.
Upon installing the app locally or globally, a script will be created in the path (or locally in node_modules/.bin), which will let your command line app be run conveniently.

Related

Is there an equivalent to java .war files for NPM?

Using gradle and java, I can build and publish (into a local repository) various versions. If a deployment has issues, I can reroll by deploying a prior version.
I can't find anything comparable for NPM applications, e.g. vue.
I foun npm pack which seems to build something that I can later install via npm install.
But I could not find out I then "run" the application. Are the any good documentations?
Thanks in advance.
Unline Jave Javascript doesn't compile into anything (because its interpreted). This means that all you need it the source code.
A basic node app source code will look something like this
main.js # entrypoint
packege.json # dependencies
src/ # application codebase
So if you like to create an artifact that can be deployed to a server you need only to download the dependencies (that's what the npm install is for) and that's it basically.
Now a node_module directory will be created
main.js # entrypoint
packege.json # dependencies
src/ # application codebase
node_module/ # dependencies codebase
You can run the application with node run main.js. But if you are creating an artifact you should bundle (zip) all the files and upload then to a file server (this will be the closest thing to .war in this flow)
You can now unzip and run the node application on a server that has NodeJS installed.

Node JS/ Angular

I have installed Node JS and then imported an existing work project in Visual Studio Code. After that I ran npm install in the project folder, then run npm start and the app comes up fine. However, if I run an ng command I get an error telling me "ng is not valid command", even though the Angular CLI is in the modules folder.
I tried manually installing Angular CLI globally and set system path to point to the npm folder, and then the ng command works fine. What I don't understand is why do I need to install CLI globally if I just want to run that command within the project where the module is already present?
As a general rule then you will need to install globally any commands you wish to use (without NPX). This isn't really a restriction of NPM so much as it is a fundamental way in which command line programs work. The OS will only look in fixed predefined locations set in PATH. This applies to any Node based tool such as grunt or ng or whatever.
(While some systems do look for executables relative to the current working directory, or can be configured to, it's generally not a good or reliable method and NPM doesn't rely on this behaviour).
For something like the Angular CLI then installing it globally should be fine and is what many people will do. As a general rule if it is a command you want to run, rather than a dependency for a project, you can consider installing it globally. You'll notice that on the Angular CLI page the example does exactly that.
In many cases however you might want to run a command from a local project. Perhaps for a build script or something else where you want to keep it isolated. In that case you instead prefix your command with npx which will look inside the local project for commands.

Installation of vue-cli : how does it work?

I'm very new to Node Package Manager and also Vue, and I'm trying to understand what exactly is going on with using the Vue CLI.
The vue.js website has this as instructions for running the official Vue CLI:
I have a few questions about this:
Does npm install --global vue-cli need to be executed only once on a machine, or once on a directory, or once per new project you're starting? In other words, once it's on your computer, is that the last time you need to run that command, or do you need to execute this command every single new project you start?
Once a new project is initiated, are local copies of the newest version of vue (and vue-router, if selected) installed?
If I finish this project and want to deploy it, how do I then port this over to a production server?
Once in a machine, except for the rare cases where one is isolating one's npm install (such as by using nodeenv or inside a container); that's what the global option is for.
After running npm install, yes.
Running npm run build and copying the contents of the resulting dist directory to the production machine (often within a /var/www directory or similar). This can be automated further in many ways.

How to run a node.js app from name

How do you create a console app that you can run by a name instead of using node and a .js file to start the app?
Instead of...
$ node myapp.js --out ./folder/
Can I...
$ myapp --out ./folder/
You can use the bin option in your package.json file to make your script executable. From the documentation for package.json:
A lot of packages have one or more executable files that they'd like
to install into the PATH. npm makes this pretty easy (in fact, it uses
this feature to install the "npm" executable.)
To use this, supply a bin field in your package.json which is a map of
command name to local file name. On install, npm will symlink that
file into prefix/bin for global installs, or ./node_modules/.bin/ for
local installs.
Click the link and search for bin: https://www.npmjs.org/doc/json.html for more info.

How can I install nodejs modules to appfog

Normally in local, I use cmd, command line to install any nodejs module using "npm install testing or connect or etc.."
But I decided to use appfog as server and I add nodejs to my project on appfg but probably I am gonna need some nodejs modules like testing, connect, request etc..
The problem is I couldnt found tool like cmd on appfog or any way to add nodejs modules.
How can I do this?
Basically, you just need to install the dependencies in your local environment and everything should be just fine. Simply specify all of your dependencies in your package.json file, run a typical npm install, and if the modules are properly installed in your local environment then they will be pushed to AppFog when you run af update.
You just have to develop your application on your local machine with all the required dependencies in the json package and modules in the Node.JS. And if your application is working properly at that time on your local machine then you can push all that application data to the AppFog using command line interface. Simply use the command af update <your app name> Then start the application and you will see that everything is working properly.

Resources