Sh script : run npm / http-server - javascript

I am writing a shell script that builds an angular app (ng build) and then runs a web server on the dist folder where angular bundles the app. The web server is run using the http-server command. (http-server just like npm needs to be installed globally in my server so the script can be run successfully.)
For some reason, I want to copy the angular source code into another server and execute the script there. I can't tell if npm/ng and http-server are installed on the other server.
If I try to install them automatically from the script (for example: npm install --global http-server) would root/admin be an issue here ?

You can't use package if it is not installed.
And no, it does not require an admin permission if you are installing this package locally in your project.
If you want to install your package globally, then you will need the admin permission.

Related

Running the server in Local Machine without showing cmd the window

I' ve created my node.js app with express, now i want to upload it on my local server machine.
I've created a bat file (I will use task planner to run this) that basically move to folder app and launch "npm run start".
The question is: There's a more correct way to this? (no cloud) and is it possible to hide the cmd window?
Putting a node.js application to production can be done with pm2. Pm2 will spin up your application and keep it alive, without having any terminal/cmd open.
Simply install pm2 globally on your machine:
$ npm install pm2 -g
Then launch your application with pm2:
$ pm2 start app.js
There are some configurations you could do, so I suggest you look at their repo

Is there a way to have NPM build a package after it is installed?

I am currently using a github url as a package which is basically a private base library for my application.
While a npm package will run the npm build script after it is installed it currently appears that if you use a github url as package no build script will be run after installation (Yes in that thread they say that if using npm 5 a prepare script will be run but from my testing it does not work).
I am wondering if it's possible to define a script in a parent npm package.json that would run the build script of a dependency after it installs?
Or if you have any better way to deal with this problem would be most welcome.

How to make a nodejs script be a CLI tool in linux?

I made a javascript file mytool.js, it has some dependencies (in package.json).
Now I can execute typing in
~/mynodes/mytool $ node mytool
But if I change the working directory I can't use this command anymore because previously it was run locally.
What I want to achieve is to be able to just type :
~$ mytool
(wherever I am in my system's filesystem and without typing node before).
Should I install it manually ?
If yes, where is the common location to install a personal nodejs script in a unix-like system ?
Or is there a npm-like command to install a personal script system
wide ?
When you add a "bin" key in your package.json:
"bin": {
"mytool": "mytool.js"
},
then you will be able to install your script with npm install -g and it will be automatically added where it should be (to a place where other globally installed cli tools are installed, which should be in your PATH).
You can see this simple project as an example:
https://github.com/rsp/node-websocket-vs-socket.io
It was created as an example for this answer but it does what you need:
it has a single script to run
it has external dependencies
it can be installed globally
it can be run from any place with a single command
Note that you don't need to publish your script to npm to be able to install it - though you can do it, or you can also install projects directly from GitHub (including private repos) - but you can also install a module that you have in your local directory or a tarball:
npm install -g module-on-npm
npm install -g user/repo-on-github
npm install -g /your/local/directory
npm install -g /your/local/tarball.tgz
For more options, see:
https://docs.npmjs.com/cli/install
Also keep in mind that for your program to be able to be executed from anywhere, you need to use paths relative to __dirname or __filename if you need to access your own files relative to your code. See:
https://nodejs.org/api/globals.html#globals_dirname
Put a shebang line at the top of the script (e.g. #!/usr/bin/env node).
Put the script in a directory in your $PATH
Give it executable permission (e.g. chmod +x /usr/local/bin/example.js)
First option:
You can run your file globally by putting on the first line of the file : #!/usr/bin/env node, copying it to /usr/local/bin and make it executable: sudo chmod +x /usr/local/bin/yourfile.js and then you can call it from where you want with yourfile.js
Second option:
Make your local file executable and create an executable bash script which calls your local file and put it in /usr/local/bin and then call the bashfile globally.

How to install Node.js or npm on Xampp

I'm new, so please excuse any mistakes.
I am trying to develop a web application locally using XAMPP. I intend to deploy using a hosted web service. XAMPP was a very convenient way to get me up and programming without any fuss.
There are several github javascript libraries that look like they can only be installed with npm. Does this mean that I cannot use them? The specific one that I'm currently having a problem with is: https://github.com/selz/plyr. Or, is it possible to install npm on my XAMPP server? I have a windows machine.
Thank you very much in advance for your help.
is it possible to install npm on my XAMPP server
You will not be installing NPM on your server, you will be installing it on your machine (think of it as a regular program you install on your PC) (if your server happens to be your PC as well, then, well, you are technically installing it on your server)
In order to be able to use NPM though, since NPM is written in JavaScript, you will need to install Node.js, also on your machine.
So, head over to the Node.js site and download and install Node. NPM is included in the installer and will be installed alongside Node.
After that is done, installing NPM packages is simply a matter of heading to your desired directory and executing npm install <PACKAGE_NAME> there.
In general, XAMPP helps you with your back end. Not your front end.
Useful links:
https://docs.npmjs.com/
Does this mean that I cannot use them?
No. You can install node at https://nodejs.org and then use npm through your windows command line to install packages. To test this, just
Install NodeJS
Open Command Line
Make a new folder under your user folder called test mkdir test
cd into that folder on Windows Command Line cd test
Create a new project with npm init -y *note this creates a
package.json file for you
Now you start installing packages with npm install <package>
--save * --save saves the package info in your package.json file (recommended). There is also -g wihch means the package will
be installed globally (usually you don't want this and only want it
locally to your project).
That's how you use NodeJS. If you want to use that with XAMPP then you need to do the same thing but instead of using your user folder (C:\users\yourname) you need to use the htdocs folder of XAMPP (usually C:\xampp\htdocs)

install sails manually on ubuntu server

My Ubuntu server is behind firewall.hence when ever I try to run command to install sails via putty using command npm install sails -g ,I get error Error: connect ECONNREFUSED.
There fore I though if I can download the sails package on to my local and then moving the package manually on firewall server and installing it. I tried looking for some help on web but could not get. Please guide how to proceed
If your computer is not connected to the internet npm will not work.
If you have another computer connected to the internet, you can run the npm commands there and afterwards copy the contents to the other computer. (e.g. via USB stick)
In most cases you should have no problems, even on different operating systems.
Note on global modules: For global modules (e.g. gulp, bower, forever) you will not be able to install into node_modules using this technique. However, you can install them locally by saving them to your package json and running them from their local path.
e.g.
npm install --save forever
./node_modules/forever/bin/forever
Check the files system for the exact path.

Categories

Resources