Express JS

Install Express JS and generator

Use the following command to install express:

  • Express installation
    • npm install express
  • Express generator installation
    • npm install express-generator -g
  • Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
  • To test the installation use following command:
    • express –version

Screen Shot 2016-07-09 at 11.08.24 PMScreen Shot 2016-07-09 at 11.10.55 PM


Create an app

  • express [your-app]

Screen Shot 2016-07-09 at 11.20.00 PM.png

  • Install required node modules
    • npm install

Screen Shot 2016-07-09 at 11.38.36 PM

  • Start the application
    • npm start
  • Open browser and enter localhost:3000. You should see the following:

Screen Shot 2016-07-09 at 11.52.17 PM

npm install [node_modules]

Checking node and npm installation

Use the following command to test if npm & node is installed:

  • node –version or node -v
  • npm –version or npm -v

Refer to this POST in case node or npm is not installed.


Initializing a node project

  • npm init
  • This will initialize node programming on current folder and will create a package.json file. Package.json will contain the list of dependencies required for application development.

Installing node pakage using npm

Use the following command to install a node package:

  • npm install [package_name]
    • This will install the specified package into the local node_modules folder.
    • Ex: npm install bower
    • Parameters:
      • -g or –global: This installs the package as a global package.
        • Ex: npm install -g gulp
      • -S, –save: Package will appear in your dependencies.
        • Ex: npm install –save gulp
      • -D, –save-dev: Package will appear in your devDependencies.
        • Ex: npm install –save-dev gulp
      • -O, –save-optional: Package will appear in your optionalDependencies.
        • Ex: npm install -save-optional gulp
      • -E, –save-exact: Saved dependencies will be configured with an exact version rather than using npm’s default semver range operator.
  • npm install
    • This will install all modules listed as dependencies in package.json.

Gulpgulp-error

Use the following command to install gulp:

  • npm install -g gulp
  • Gulp is the streaming build system. It is recommended to install it globally as it can be required on multiple projects.

Bowerbower-logo

Use the following command to install gulp:

  • npm install -g bower
  • Bower is a package manager for the web. It is recommended to install it globally as it can be required on multiple projects.

express_jsExpress

Use the following command to install express:

  • npm install express
  • npm install express-generator -g
  • Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Lodashlogo202

Use the following command to install lodash:

  • npm i –save lodash
  • A modern JavaScript utility library delivering modularity, performance, & extras. Lodash makes JavaScript easier by taking the hassle out of working with arrays,
    numbers, objects, strings, etc.

angularjs

Use the following command to install angularjs:

  • npm install angular
  • Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It is recommended to install it locally as it is required on the production code.

Many more libraries can be added from NPMJS site.

Refer to npm-install page for more details.

Fixing npm permission issue

Node and npm can be installed easily. However, we might face issues while using npm. Most of the time this mean that we don’t have access to install the files on the machine. This can be resolved by appending sudo command but it is not recommended as it might create other issues. If the user does not have permissions to install file and “npm install” is ran on the terminal; we’ll see the following screen:

Screen Shot 2016-07-09 at 4.49.39 PM

This can be resolved by following methods:

  1. Appending sudo:
    • sudo npm install
    • enter system password
  2. Changing the permission on global and local folders where the node modules reside:
    • Run “npm config get prefix” in your terminal. This will give the path of global node_modules: For ex: /usr/local
    • Change the user permissions for this folder by using following command:
      • sudo chown -R <user_id> /usr/local/

  3. Navigate to your application folder where your local node_modules reside.
    • Run the similar command
      • sudo chown -R <user_id> node_modules/
  4. “npm install” should work on this application from now onwards.

PS: Repeat Step 3. in case of a different project.

For more information and alternatives visit Fixing npm permissions.

How to install NodeJS

NodeJS: Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

  1. Visit the NodeJS site
  2. Download the package suitable for your OS.
  3. Double click the package to begin installation.
  4. Follow the wizard to complete the installation process. You’ll be prompted for agreeing with legal T&C’s and system password.
  5. A summary of installation will be displayed in the last step. As seen in the above image Node.js and npm were installed successfully at the mentioned path. The path might be different depending on your settings or OS. Node.js package comes along with its own package manager: Node Package Manager or npm. Visit NPMJS site to know more about npm.
  6. To test the installation open your terminal or command prompt and run following command:
    • node –version or node -v Screen Shot 2016-07-09 at 4.23.13 PM
  7. npm installation can be tested in similar way:
    • npm –version or npm -v Screen Shot 2016-07-09 at 4.32.38 PM.png

Hope this helps you with your Node installation.