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.
Related
I am making a web page, node_modules file is around 150 megabytes, should I upload it or not? Is there any way to make it smaller? I am using "filezilla" and it would take too long to upload it.
Node modules is where all the external libraries you use for your application are kept. The list of those libraries should be mentioned in package.json file.
You should, typically, not upload node modules folder manually. They are the external libraries and are easily available to install separately. So, when moving files through filezilla, move everything but node modules. Then, in your server, simple run npm i before running the application.
If you have a package.json file and used npm module -s <package_name> (with -s or --save) then everything was fine.
If you don't have it no worries.Transfer the files into your online services like AWS,Something like that.
Then give the commands.
// For install npm
npm i
(or)
npm install
// To start your server
npm start
Whatever you put in your package.json file Start Object the file will be triggered.
No need to copy the node_modules folder at all.
I have installed npm-check-updates on my Ubuntu 18.04 system with npm install -g npm-check-updates. This package requires you to run it with $ ncu from the command line.
But when I try to run it using ncu the shell just executes a different weather update program that runs with the ncu command as well.
Is there a way to specify that you are trying to run the npm executable specifically? Thank you.
I want to run the npm executable and not the binary located at /usr/local/bin/ncu
Run whereis to find where the binary images are.
⇒ whereis ncu
ncu: /usr/bin/ncu /usr/local/bin/ncu
For example, in my computer there is two ncus located at /usr/bin/ncu and /usr/local/bin/ncu.
Then you could run it by its full path like:
⇒ /usr/bin/ncu
No package.json
...
For more convenience you could define an alias for it in your ~/.bashrc or ~/.zshrc like:
alias ncu2="/usr/bin/ncu"
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)
I am attempting to install the node.js module 'javascripting' (source code can be found: https://github.com/sethvincent/javascripting) and have been unable to install it as a global variable to run through the terminal.
After installing node.js I attempted to install javascripting with the line: npm install --global javascripting
While it is my understand that this should work, it only downloads the module but does not set it as a global variable to be run in terminal.
The error I receive when attempting to run it as a global variable is "/usr/bin/env: node: No such file or directory".
After receiving this error I attempted to move the module to /usr/bin/env from the directory it installed in (usr/local/lib/node_modules/javascripting). Unfortunately, I was not able to move the files because /usr/bin/env is not a directory, rather it seems to be some sort of executable java file (usr/bin is a directory).
I am a bit lost and would love some advice on either how to install the module as a working global variable or whether there is another way to run the module without installing it as a global variable.
This will happen if the node.js binary (node) is not installed in the $PATH anywhere.
if you run env node by itself, you will get the same error. It looks like this may be an Ubuntu bug: https://github.com/joyent/node/issues/3911
Try sudo ln -s /usr/bin/nodejs /usr/bin/node - that will symlink the node.js binary from the name Ubuntu gave it to the name it's supposed to have.
EDIT:
As mscdex pointed out in a comment (and as mentioned at the end of the bug I linked), there's a legacy package you can install that should create this symlink.
sudo apt-get install nodejs-legacy
The bug I linked above indicates that there are probably other problems with Ubuntu / Debian's default node.js package, and recommends you install your own either from the PPA mentioned there or from source.
You'll probably need to follow the advice in NPM modules won't install globally without sudo as well.
Setting npm up as the root user is straighforward and workds. Except you have to run npm commands as root (not recommended). So I thought I'd try setting it up as a non-root user.
According to npm documentation, a non-root user without root access can set up npm by:
creating a .npmrc file with root, binroot, and manroot pointing to folders that the user owns.
Then running the install script.
OK. Install was fine.
But node can't see the packages provided by npm.
So how do I make node aware of the packages provided by npm? (I didn't have to do anything when I previously installed npm as root).
I can set require.paths within node, or set the NODE_PATH environment variable, but to what?
Thanks.
This worked for me:
Make a ~/.node folder
mkdir ~/.node
Edit ~/.npmrc and add the line
prefix = ~/.node
Edit your ~/.profile or ~/.bash_profile and add these lines
PATH="$HOME/.node/bin:$PATH"
NODE_PATH="$HOME/.node/lib/node_modules:$NODE_PATH"
Now I can do things like npm -g install http-server and it will install to ~/.node without root. With this in place, when I then type http-server, it runs.
Through a little exploration, it seems you can either:
set your NODE_PATH to whatever npm's root directory is, or
while in node, invoke require.paths.push('path_to_npm_root')