Node http-server not working on Ubuntu linux - node.js

I am trying to run a simple http server in my project directory. All I need is GET request support, so I can GET html/css/js/etc.
For that I wanted to use http-server from npm.
I installed it with npm install http-server -g
Now I cd to my project folder where it has the index.html file, I open the terminal and run http-server
But when I open my browser at http://localhost:8080/index.html - it can't connect to the host.
Am I missing something?

Okay, the issue was - I had another package installed on Ubuntu, which is also called node
Node JS package is called nodejs on my system and I think that http-server is looking specifically for 'node'.
In order to work around this:
I removed the node package with sudo apt-get remove node and created a symlink for nodejs:
sudo ln -s /usr/bin/nodejs /usr/local/bin/node

First install npm.
Second npm install http-server -g.Next append after the http-server url template url like http-server C:\xampp\htdocs\

Related

Run Laravel Mix without a global nodejs and npm installation

I have a laravel project and I need to build styles and scripts with laravel-mix, but a testing server (Ubuntu 20.04.4) hasn't a globally installed node. Node and npm are in different folders in the system so I run commands like this:
/path/to/node /path/to/npm install
/path/to/node /path/to/npm run dev
But when I run npm run dev (this command runs laravel-mix build), I see the error:
> mazer#2.0.0 dev
> mix
/usr/bin/env: ‘node’: No such file or directory
In the package.json it looks like this:
"scripts": {
"dev": "mix"
...
}
I checked the laravel-mix package (in node_modules) and found this: #!/usr/bin/env node. The package checks the node var in this file, but there is no node var.
I don't need to change the env file, so how can I change this path or set a temporary system var? Is there any way to simulate that the variable is there?
I have one solution for this problem.
The issue regarding naming misspelling or path symlinks.
so that you need to link symlinks for nodejs with this command
ln -s /usr/bin/nodejs /usr/bin/node
or
sudo ln -s /usr/bin/nodejs /usr/bin/node
I resolved my issue with Docker, so now I run this command on git push:
docker run --rm -v /path/to/project:/var/www/html node:16.16.0-alpine npm run dev --prefix /var/www/html
Perhaps it will be useful to someone.
UPD
I found another way to resolve it, I use PATH incorrectly and for this reason it didn't work:
Wrong
I set paths to node and npm and then add it to PATH like this:
NODE_PATH="/path/to/node_folder/node"
NPM_PATH="/path/to/node_folder/npm"
PATH="${NODE_PATH}:${NPM_PATH}:$PATH"
And the system can't find npm and node anyway.
The right way
Add /path/to/node_folders (node and npm are in it) to PATH:
NODE_DIR="/path/to/node_folder"
PATH="${NODE_DIR}:$PATH"
And then, I can run just npm install and npm run dev without full paths to them.

Create vue Permission denied

I'm trying to install vue app using this command "npm init vue#latest",
But the following error occurred with me, any help :(
Note : I tried many solutions like : chmod -R 755 .npm and sudo chown -R mostafa .npm but nothing changes
maybe you could try using the vue-cli like this:
npm install -g #vue/cli && vue create hello-world
If this wouldn't work then I'd suggest to reinstall node/npm
You should probably also make a clean installation of these tools so that you don't need to run such things as administrator
In order to install vue on your machine you must first have installed nodejs ( package manager node package manager ) to which the acronym npm refers, I leave you the linux commands under :
install :
sudo apt install nodejs
shows the installed version of nodejs :
node -v
After installing nodejs try relaunching the command to install vue
if you have already installed them try with this :
add write permissions to the study folder , and not to the mostafa folder , linux only assigns permissions to the folder in the chmod command and not subfolders, try these commands :
cd home/mostafa/Dowloads
sudo chmod ugo+w study
Are you not being blocked by SELinux directives?
Try running the same command using your least privileged user (normal user).
And please avoid using the root user for everyday tasks.
You need to have installed Nodejs, vue and create-vue.
sudo apt install nodejs
npm install vue#latest
npm install create-vue#latest
Now you can run the command to start the new project with Vue.
npm init vue#latest

How do I know whether I have Node.js and npm successfully installed on Ubuntu 14.04?

I installed Node.js with these instructions and it seemed successful:
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
Then I installed npm with these instructions:
sudo curl https://www.npmjs.org/install.sh | sh
The nodejs installation seemed to work without errors but the npm command gave me a lot of errors. But it seems like they are installed because when I test what version I have they both come up:
nodejs -v
v0.10.30
npm -v
1.4.21
So If this doesn't tell me that I have both programs successfully installed, which I assume I do not, how do I know?
I think your tests tell that both or properly installed.
But you can try just type node in terminal & it should open a node shell, where you can check by running basic commands.
Current distributions of node.js (including the one you downloaded) already include npm. So maybe installing npm manually is one source of your errors. Beware that usually you run "npm install" with the permissions of a regular user. There are only some npm-based utilities that are to be installed with root permissions and the '-g' (global) command line switch.
On linux if you wish to install node.js and npm as yourself NOT root :
to start fresh remove prior node.js and npm installs as well as these :
~/.npmrc
~/.npm
~/tmp
~/.npm-init.js
create your ~/bin/ directory if not already created :
mkdir ${HOME}/bin
download source from : http://nodejs.org/download/
cd node-v0.10.30/
./configure --prefix=${HOME}/bin/nodejs
make -j8
make install
which puts it into dir defined by above --prefix
export PATH=${HOME}/bin/nodejs/bin:$PATH
define NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into dir in curr dir :
export NODE_PATH=${HOME}/bin/nodejs/lib/node_modules
do above AND use syntax :
npm install xxxxx -g
always use the -g for global which puts package xxxxx into $NODE_PATH
NOTE - nodejs install gives you npm as well :
ls -la ${HOME}/bin/nodejs/bin

Express command not working in terminal

I am kind of new to node.js. I installed node fine and it works. I can run the node command in the terminal as well as the node package manager command(npm). Working on a Mac by the way. So I installed express globally with the following command:
sudo npm install -g express
After I was done installing it globally I installed it in a small folder where I am working on a project.
sudo install express
That gave me a node_modules folder inside of my site folder. After I was done that I went to install stylus using the express command and I got the error that express is not a command.
express -c stylus
Can anyone shed some light on this issue? I also tried to link the global express to the folder where I am working on the project using the following command:
sudo npm link express
The terminal said that this command ran, but I still cannot run the express command.
Thanks for asking questions on this one. I found that running express-generator instead of just express fixed the problem. The following code in the terminal worked.
sudo npm install -g express-generator
Thanks again,
If your express binary keeps doing nothing. Install node-legacy:
sudo apt-get install nodejs-legacy
on ubuntu 14.04 nodejs 0.10 express did not work on terminal, though
sudo apt-get install nodejs-legacy
made it to work.
I faced similar issue. I tried all the solutions mentioned here. However couldn't get my problem solved.
This happens when location of express and nodejs is not same.
Issue arises when Node is installed using homebrew.
Just use this command export PATH=/usr/local/share/npm/bin:$PATH
NOTE: replace path with the path of express.
Follow this link for help.
Make sure you have package.json & index.js files. And instaled express-generator with -g tag.
If still getting error use like ex:npx express --view=pug myapp --git
I fixed this on my mac by forcing my user to own /usr/local/lib and running the command to install express-generator globally:
sudo chown -R $USER /usr/local/lib
then:
npm install -g express-generator
After this you can go ahead and run your express commands:
express -h

node.js: cannot find module 'request'

I installed request module, and getting the error:
module.js:340
throw err;
^
Error: Cannot find module 'request'
i've read all the posts about this error, and understand that this is because module requests is not globally found, but i've already tried the 2 suggestions
npm install request -g
should this install it in /usr/loca/bin ? because i don't see it there.
and
sudo npm link
/usr/local/lib/node_modules/request -> /Users/soulsonic/dev/sandbox/node_test/request
i restarted terminal after each command, but keep getting the cannot find module error.
update
there must have been some sort of conflict in my initial directory, because "npm install request" was not adding "request" under node_modules (there 10 others in there) ..
after switching to a new directory it just worked.
if i run it with -g switch, i do see it bing installed to /usr/local/lib/node_modules/request.
it seems that i just need to update my profile so that above path is automatically added.
Go to directory of your project
mkdir TestProject
cd TestProject
Make this directory a root of your project (this will create a default package.json file)
npm init --yes
Install required npm module and save it as a project dependency (it will appear in package.json)
npm install request --save
Create a test.js file in project directory with code from package example
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Print the google web page.
}
});
Your project directory should look like this
TestProject/
- node_modules/
- package.json
- test.js
Now just run node inside your project directory
node test.js
You should simply install request locally within your project.
Just cd to the folder containing your js file and run
npm install request
I had same problem, for me npm install request --save solved the problem. Hope it helps.
I have met the same problem as I install it globally, then I try to install it locally, and it work.
if some module you cant find, try with Static URI, for example:
var Mustache = require("/media/fabio/Datos/Express/2_required_a_module/node_modules/mustache/mustache.js");
This example, run on Ubuntu Gnome 16.04 of 64 bits, node -v: v4.2.6, npm: 3.5.2
Refer to: Blog of Ben Nadel
I tried installing the module locally with version and it worked!!
npm install request#^2.*
Thanks.
I was running into the same problem, here is how I got it working..
open terminal:
mkdir testExpress
cd testExpress
npm install request
or
sudo npm install -g request // If you would like to globally install.
now don't use
node app.js or node test.js, you will run into this problem doing so. You can also print the problem that is being cause by using this command.. "node -p app.js"
The above command to start nodeJs has been deprecated. Instead use
npm start
You should see this..
testExpress#0.0.0 start /Users/{username}/testExpress
node ./bin/www
Open your web browser and check for localhost:3000
You should see Express install (Welcome to Express)
ReferenceError: Can't find variable: require.
You have installed "npm", you can run as normal the script to a "localhost" "127.0.0.1".
When you use the http.clientRequest() with "options" in a "npm" you need to install "RequireJS" inside of the module.
A module is any file or directory in the node_modules directory that can be loaded by the Node.
Install "RequiereJS" for to make work the http.clientRequest(options).

Resources