Set up Express.js using npm

Just set up nodejs on my Ubuntu 14.04 system. Upon running node --version, I see that it's v4.4.2. Also, npm is installed with version 3.9.2. When I execute the command npm install -g express, this is the output I get: install express js

Once the installation process is complete and I check the express version, the message displayed says The program 'express' is not installed. Please try apt-get install node-express. Can someone point out where I might be going wrong?

Answer №1

Guide to Installing Express on Ubuntu using npm (Follow the Steps Below)

To begin, open your terminal using 'ctrl + alt + t'

  1. Check if nodejs is installed by running the command: node -v
  2. Navigate to the path in the terminal: cd /var/www/html
  3. Create a new folder either manually or via command line: mkdir nodeTest
  4. Enter the 'nodeTest' folder: cd nodeTest
  5. Initialize a new Node project with 'package.json': npm init
  6. Install Express: sudo npm install express --save

Now access the 'nodeTest' folder at: /var/www/html/nodeTest

Create a new file: index.js

index.js

var express = required("express");
var app = express();

app.get("/", function (req, res) {
    res.send(" Welcome Node js ");
});  

app.listen(8000, function () {
    console.log("Node server is running on port 8000...");
});

To start the node server, use the terminal command: node index.js

Then check the url: "localhost:8000"

Answer №2

As mentioned in my previous comment, ExpressJS is a framework designed for building servers rather than being a server itself.

To start using ExpressJS, first create an npm project by running npm init, then install it as a dependency with npm install express --save. Next, take a look at the "Hello World" example to learn how to set up a basic server: Hello World Starter

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

If you need to serve static files, you can do so by adding the following code:

app.use(express.static('name_of_the_directory'));

Answer №3

The tutorial you're currently using may not be up to date.

In the past, Express used to come with a command line tool called express, which allowed users to create application skeletons. However, this tool was removed from the main express package after the release of Express 4. It has now been moved to a separate package known as express-generator.

To install this package, you can use the following command:

$ npm install express-generator -g

Once installed, you should have access to the express command line tool. Keep in mind that due to the outdated nature of the tutorial, you may encounter additional issues along the way.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on utilizing a dual-parameter route in Node.js with the GET method

Need help with using 2 parameters in 1 route (get)? Check out my code below: router.get('/', function (request, response) { Result.find(function(error, results){ if (error) console.log(error) response.render('index&a ...

What is the best way to trigger a complete page reload following a redirect to an updated version of the same page?

I've encountered a challenge with Express (utilizing Cloud Firestore), and my search on SO did not provide any viable solutions (despite the discussions in these threads) Outlined below is a straightforward route (all aspects are handled server-side) ...

Storing values globally in NodeJS from request headers

What is the most effective way to store and access the value from a request header in multiple parts of my application? One approach could be as shown in the following example from app.js: app.get('*', (req, res) => { global.exampleHeader ...

Parsing DOM data from an HTTP request in Node.js

Looking to extract data attributes from an SVG element on github.com/profile_name using a node.js HTTP request, and then parsing it into JSON format. <rect class="day" width="10" height="10" x="-36" y="10" fill="#ebedf0" data-count="0" data-date="2017- ...

Issues with installing mongoose on Windows 7 using npm

Struggling to set up mongoose on Windows 7, I've exhausted all resources on Stack Overflow related to my issues with no success. Upgraded npm version to 2.4.1 If anyone can offer assistance, it would be greatly appreciated. Here is the error log: F ...

The request in C++ is failing to reach the intended computer

Trying to utilize libcurl with C++ for sending an HTTP request to a remote host (my friend's computer) over the internet. On the receiving end, Node.js script is used to handle this request. I have successfully tested this code (both C++ and Node.js) ...

Guide on setting up @types from an NPM module containing the "@" symbol in its title

Installing the node package is easy npm install @gamestdio/timer --save But when attempting to add the corresponding types npm install @types/@gamestdio/timer --save An error occurs Invalid package name "@types/": name can only include URL-friendly ch ...

Utilizing TypeScript namespaced classes as external modules in Node.js: A step-by-step guide

My current dilemma involves using namespaced TypeScript classes as external modules in Node.js. Many suggest that it simply can't be done and advise against using namespaces altogether. However, our extensive codebase is structured using namespaces, ...

Tips for correctly defining the file path in Linux when working with Node.js

I am attempting to access a file: fs.readFileSync('~/node_modules/.bin/lasttest.txt', "UTF-8"); However, I receive an error stating that the file or directory does not exist. Interestingly, when I execute the command: sudo nano ~/node_modules/ ...

Users are directed to various pages based on their specific roles

I am currently working on implementing a simple authorization system using an array to simulate a database. Let's say I have an array with information about two individuals: const users = [{ info: 'First person', login: &apos ...

Node.js not resetting array properly

I have successfully set up a Node+Express API that is working smoothly, but I am facing an issue with returning responses for complex queries. The problem lies in the fact that the variable where I store the response data is not being reset between differe ...

Guide for implementing express middleware at the router level

I am currently attempting to implement a basic middleware function that will run for every request at the router level. According to the documentation: a middleware function with no mount path will be executed for every request to the router In my appl ...

Passport.js will direct you to a "302 Found" page following the authentication process

Passport.js allows users to specify success and failure redirection URLs after authentication: app.post('/login', passport.authenticate('local', { successRedirect: '/success.html', failureR ...

The error message "Error: 'x' is not a defined function or its output is not iterable"

While experimenting, I accidentally discovered that the following code snippet causes an error in V8 (Chrome, Node.js, etc): for (let val of Symbol()) { /*...*/ } TypeError: Symbol is not a function or its return value is not iterable I also found out ...

Failed to install angular-cli globally on the system

When attempting to globally install angular-cli, I encountered some errors. What steps should I take? C:\Users\Jahidul>npm install -g angular-cli npm WARN registry Using stale data from http://registry.npmjs.org/ because the host is inaccess ...

Debugging in Node.js - automatic error message generation

Hello, I am a beginner in the world of nodejs (async) debugging and could really use some assistance when it comes to dealing with error messages. var l, m, opiton= {}; // It appears that the variable 'option' has been mistakenly spelled as &apo ...

Managing Events in EJS

I am currently utilizing the combination of Node.js, Express.js, and Ejs. Challenge In my particular scenario, I have 3 distinct layers: 1) clients-side.js ((node.js) 2) server.js (node.js) 3) front-end.ejs (ejs) Upon successful form submission, an e ...

Tips on executing npm commands on Azure app service following a successful deployment via VSTS?

While I've successfully deployed from VSTS to Azure, I'm facing an issue with running npm after the deploy is complete. The current process involves running npm install for branch files, zipping them, copying to Azure, and deploying. However, I ...

The NPM command is functioning properly with CMD, but I'm encountering some issues when trying to use it with BASH. Any suggestions

Currently, I am following a tutorial to develop a simple TODO app using the MERN stack. The process was smooth while setting up the server with Express in the bash terminal. Here is a snippet of my server file: const express = require('express') ...

Hyperledger Fabric: ERROR! 404 - The requested fabric-chaincode-api package (version 1.4.5) could not be found on the npm registry

I created my chaincode using Node.js API and now I'm attempting to perform the instantiation process. docker image: hyperledger/fabric-peer:1.4.5 In my package.json file, here is what I have included: { "name": "democontract", "version": "1 ...