ExpressJS installation methods - node.js

Firstly, I understand that since version 4 (express), the boilerplate HTML generator has been splintered off. However, does executing the command...
npm install -g express-generator
... and not...
npm install -g express
... automatically install the core express engine as well? I am asking this because, surprisingly, I was only instructed to install the first.
Secondly, an online tutorial instructs something similar to the following...
npm install -g express-generator
express --ejs pineapple-news
cd pineapple-news
npm install
However, on the contrary, I found that the official express documentation recommended this instead...
mkdir pineapple-news
cd pineapple-news
npm init
... hitting return for all the defaults and then ending with...
npm install express --save
As a newbie, I need to understand the differences between the two approaches and the reason for the differences. Any pointers would be highly appreciated.

Let me explain the two cases.
First case :
npm install -g express-generator
express --ejs pineapple-news
cd pineapple-news
npm install
Here, with the first line, you're installing express-generator globally. That is, after installing it globally, you can use it like any other program that can be invoked from the terminal. In the second line, you are invoking the express generator and specifying ejs as the template engine and pineapple-news as the project name. So, the express generator creates a folder named pineapple-express with the necessary sub folders, a default app.js file and a default package.json file. The package.json file thus created will have all the dependecies including express and ejs, the template engine to be used listed in it. Then, when you execute npm intall ,all the dependencies will be installed automatically.
Second case:
mkdir pineapple-news
cd pineapple-news
npm init
With the first and second line, you're creating a folder with the name as pineapple-express and entering inside it. The npm init command creates the package.json file after prompting you to specify some parameters such as project name, version, repository etc. The package.json file thus created will not have any dependencies listed in it. When you do npm install express --save, it will add express as a dependency in the package.json file and install express in the pineapple-express folder. I hope things are clear now.
In first case, all the necessary folder structure and and app.js file with the all the necessary settings to start an express application is also generated. It contains the configuration related to template engine, static file path, cookies configuration etc. Some of the dependencies like morgan, jade template engine etc are also installed by the express-generator.
In the second case, you will have to create the app.js file and write the configurations manually. This includes creating the express app.
The app.js(you can name it anything like server.js or engine.js) file contains the configuration for starting a node js server. This file is executed with node to start the server.

npm install -g install the package globally on your system. express-generator is a module that setup a directory to contain your express app. Creates a directory structure and adds a package.json file with express as a dependency plus other common used modules. So you just can start to write your app. Express-generator install express just locally on your app directory. that's why you have to run npm install on the created directory after creating the directory with express myapp

Related

How to install local npm automatically?

I developed an application in NodeJS which use external npm which are not published on npm.
I've actually separated my code in components, so the app have this structure:
/app
index.js
/components
/componentA
index.js
/componentB
index.js
so essentially when I publish the app on my server, I did:
npm i
npm i ../components/componentA
npm i ../components/componentB
as you can see this could be problematic if the app is a big project and have hundreds of components. So my question is, how can I execute a script that check automatically if the components are installed and automatically install it?
I want avoid service like bit.dev or something like. I also used npm link but unfortunately when I install a new package my own npm are automatically removed.
If you have a package.json in your project, you can run npm i -S ../components/componentA which will add it as a dependency in your package.json.
Your package.json should look like
"dependencies": {
"componentA": "file:../components/componentA",
"componentB": "file:../components/componentB"
}
On server just run npm i to get these components installed

How much damage can I do by installing a module in the wrong place with npm?

I've got an iOS app, using google cloud functions and want to install the Request module/package https://www.npmjs.com/package/request in the node_modules folder.
My folder structure is:
- Desktop
- myApp
- firebase
- functions
- node_modules
I was reading up on npm about npm install and there was some stuff about installing locally, globally and some other things I didn't quite get.
Is there a standard way to install packages with npm?
And if I wanted to install "Request" in the terminal do i "cd" to the node_modules folder and npm install from there or is it from within the functions folder?
Thanks.
In the same directory where you have the node_module directory (i.e. in your functions directory) you should also have package.json file. If you are in that directory and you run npm install request --save then you will install the module and put it in the dependencies in package.json file. That is what installing locally means. For more info see:
https://cloud.google.com/functions/docs/writing/dependencies
(Cloud functions for Firebase work pretty much the same as Google Cloud Functions)
Update
The --save is the default behavior of npm install since v5.0 so you don't need to use the --save flag since the saving is done automatically (there is --no-save to prevent saving).
See the release notes for more info:
https://github.com/npm/npm/releases/tag/v5.0.0

What does the -g option for npm install and npm list do?

The Node.js npm (Node Package Manager) has a -g command line argument, which I often see referenced. For example, the documentation for the Microsoft Azure x-plat (cross-platform) CLI tool says to install it by using npm install -g azure-cli.
Question: What does the -g option do?
What options do I have to install node modules?
After writing this I quickly found and old but still applicable post by Isaac (yes, the npm #isaacs). But I still think the below post is informational.
You can install npm modules globally or locally - you already know that, but why?
Globally: npm install -g some-module-a: This module is intended to be used as an executable (i.e. CLI, file watcher, code minifier, logger, etc.).
Locally: npm install some-module-b: To be imported and used in your app via import, var someModule = require('some-module)
global modules are one of the best ideas of npm. We can easily create executables using node/javascript. If your node app is meant to be run as an executable, then you will want others to install it globally. If it's a utility, helper, application, etc. then you usually don't want it installed globally. So, unless the module explicitly states that you should install it with -g, then don't.
One more time: if you are wanting to use some module called some-module in your node app - var someModule = require('some-module'), then npm install some-module from the root of your node app to pull it into your local node_modules directory. If you've installed some-module globally and not locally, it will usually not load and will show you an error about not finding the module (even though it can be made to load the global module - hint: just don't!)
So what exactly happens when you install globally?
npm install -g [some module] installs the specified node module in a directory higher up in your file system (i.e. usually /usr/local/lib/node_modules in unix systems). The biggest use case for global modules is for CLIs written using node (think npm, bower, gulp, grunt, et. al.).
Let's look at what happens when you install bower globally:
*follow these steps in your command line/terminal
step: npm install -g bower
explanation: the module - all of it's files and dependencies - are saved in your global directory (e.g. /usr/local/lib/node_modules/bower).
Something else happened here. Somehow you can now run bower in your command line. Awesome!
step: bower -v --> results in the installed bower version (i.e. 1.6.5)
explanation: It's now a fully executable node app using bower as the keyword. Inside bower's package.json file you'll find a bin property:
"bin": {
"bower": "bin/bower"
}
So how did that all work?
npm will create a symlink from where most executables live, /usr/local/bin/bower over to /usr/local/lib/node_modules/bower/bin/bower, where the module lives. That symlink makes it so when the executable runs, it can reference other files in the original module, including it's local node_modules. Pretty cool, huh?
*Note on executables: If you create a file called awesomeness in /usr/local/bin/ and chmod u+x (user + executable) it. Then write some scripting in it (in this case javascript using #!/usr/bin/env node at the top). Then you can run it anywhere in your command line/terminal just by typing awesomeness.
Hope that helped. I know doing a deeper dive into it helped me early on.
Node.js packages can be installed one of two ways:
Globally
Locally
The -g option instructs npm to install the package globally. You would install a Node.js package globally, if you want to be able to call the command directly from the terminal.
From the documentation:
There are two ways to install npm packages: locally or globally. You choose which kind of installation to use based on how you want to use the package.
If you want to use it as a command line tool, something like the grunt CLI, then you can want to install it globally. On the other hand, if you want to depend on the package from your own module using something like Node's require, then you want to install locally.
To download packages globally, you simply use the command npm install -g , e.g.:

Path of the express

Why express command not found. Here is the path for express.
/usr/lib/node_modules/express
When I install the express, the terminal shows the path. I used
npm install -g express-generator
npm install -g express
But when I run express, it doesn't work. In this directory, express is globally right? But why can't be found. I don't understand the logic.
You need to install express locally, rather than globally.
http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation
In general, the rule of thumb is:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
Based on this, you want to install express-generator using the -g flag as you will use it as a command line tool, but you want to install express without this flag as it's a module you will want to require() it in your application.
Take a look at the installation guide:
http://expressjs.com/starter/generator.html
It says to install the express-generator module as a global module as you will use it as a command line utility to scaffold your new applications.
Once you have scaffolded an application using the express myapp command, you just have to run npm install in the myapp directory which will download the rest of the dependencies to your projects local ./node_modules directory. It does this by reading the contents of the generated package.json file.
Lesson to take away: Don't install with the -g flag unless the modules instruction guide explicitly says to do so.

NPM basics with nave and Node.js

I recently installed node.js and was told that express was the way to go for routing and getting set up with web application development.
I installed the latest version of node which apparently is incompatible with the latest express.
I looked up and found nave... Like RVM, nave allows you to switch versions of node. So I ran nave.sh install 0.4.11... That worked successfully and I was able to run.
npm install express -g
This I thought, should install express globally. So I run:
express testapp
which creates
create : testapp
create : testapp/package.json
create : testapp/app.js
create : testapp/public/stylesheets
create : testapp/public/stylesheets/style.css
create : testapp/public/images
create : testapp/public/javascripts
create : testapp/views
create : testapp/views/layout.jade
create : testapp/views/index.jade
Then I
cd testapp/
node app.js
I get
Error: Cannot find module 'express'
Is this usual behavior?
Since express is in packages.json, if I run npm install -d, it will create a node_modules directory in my application and not just symlink to the node_modules in my node path.
In a word, yes, this is the usual behavior.
When you install packages using NPM with -g option, it installs it globally, which does nice things like putting executeables on your path (i.e. the express script you used)
However, it does NOT put those packages anywhere that node can find them.
To install it so node can find the package, you must also do
cd "your express app"
npm install express
which installs locally (to the node_modules folder in the root of your application dir).
This is primarily to avoid any dependencies conflicts, and though it may seem silly, it is in fact really useful.
If you have some real reason to want to use your global install (say for example you have many applications that you want to make sure always share the same version) you can use the npm link command.
For a good rundown of NPM and global vs local see this blog post.
If you are on Windows, add location to your path.
export NODE_PATH="C:\Users\IMarek\AppData\Roaming\npm\node_modules"
Change: IMarek to your user name.

Resources