express or express-generator: do i need both?

9.8k views Asked by At

Just exploring node.js and came across express; On the npm repository site https://www.npmjs.com/package/express it clearly states installation is

$ npm install express

but if i scroll down the quick start mentions

$ npm install -g express-generator@4

Can someone explain in detail what is going on? I googled and understand in express v3 both were bundled together, but in express v4 the generator was pulled out.

So do i need to install both? does the generator install express automatically but not the other way around? I have JS fatigue already, and i only just started :-(

Supplementary Question: Then to confuse matters even further, i saw another question that asks should I start with 'npm init' and require express, or 'express myApp'. What is really going on now, where does init come into it?

Update 30/Jan/2017 I've accepted an answer below from someone else, but I've also added my own answer which is what i was seeking back then as a confused complete beginner. It may help others.

3

There are 3 answers

1
IAmDranged On BEST ANSWER

My understanding is:

  • The express package is the framework that exposes functionalities you can use in your code
  • The express-generator package a utility that provides a command-line tool you can use to scaffold your project - ie create boilerplate folder structure, files and code.

As part of the boiler plate files is a package.json file defining the dependencies for your project - ie npm packages that you will need for your project. The express package is listed there.

Knowing the npm install instruction (run with current working directory set to project folder containing the package.json) will "install" all dependencies listed in package.json into your project folder to make them available to your application, it would be sufficient to do:

  • npm install express-generator -g
  • npm install
1
Codesingh On

You can use both and you won't be able to find any difference between express and express-generator but, many other modules like middleware were not necessary main express so they were added as seperate module and generator is one of those.

This idea of sepearation made express lightweight and manages to generate things efficiently.

In V4, generator is not bindded with express and you need to download two seperate modules.

0
joedotnot On

This answer refers to 'express' v4 on Windows. I don't know what the behavior was with express 3 or less.

0) open a cmd prompt as administrator

1) install express-generator globally;

npm install -g express-generator

2) generate the boilerplate files in your chosen directory, for example

express myApp

3) cd to the myApp folder, where you will find the package.json (+ your main app.js file, + other folders)

4) Finally install 'express' local to your app folder (+ any other dependencies as defined in package.json)

npm install

Notes: the express you are installing in step 4) refers to the set of javascript files which form part of the express web framework, to be used and referenced by your own app; the express referred to in step 2) is actually the express-generator cmd line which you installed globally in step 1).

As for my supplementary question, npm init is used to create a package.json file where you respond to prompts, npm init -y creates the package.json automatically with default values, in either case it is not related to express at all.

If you want to build your project from scratch without boilerplate files / folders, first npm init, then npm install --save express, this installs express locally to your app, the --save option adds express as a dependency in your package.json.

Bottom line, to use the express web framework, you don't have to install express-generator, but you must install express. And if you work on, say, 3 apps which use express, the easiest thing to do is to install express 3 times locally to each app.