Configuring a Node JS App to Use NPM - node.js

I have a really simple Node JS app and I'd like to include the Express JS framework. I've installed Express with NPM (and NPM with Homebrew) without any errors using:
brew install npm
npm install express
And my server.js file contains only:
var express = require('express');
When I run my application I get Error: Cannot find module 'express'. How can I tell my Node application to include the library?

You need to tell node where your libs are.
extract from http://nodejs.org/api.html
require.paths
An array of search paths for require(). This array can be modified to add custom paths.
Example: add a new path to the beginning of the search list
require.paths.unshift('/usr/local/node');

Here's a walkthrough of using npm's bundle command:
http://intridea.com/2010/8/24/using-npm-with-heroku-node-js?blog=company

Related

How to identify the running express.js version in code?

Can I identify the running version of express.js from within code?
For example, I can use process.version to get the Node version, and process.versions for the various dependancies.
Is there a way I can do this for Express?
Thanks
In order to find the current version of your Express JS, open your terminal and type:
npm list express
Another way is to open up your codebase and check the package.json file where you will be able to see all the packages installed along with its version number.
for local package use command npm list
for globally installed packages use command npm list -g
a specific package by passing its name as an argument use command npm list express
For getting the express version you can use
read-package-json
module. This will give the data of package.json and you can read the value of express version from it.
for more details, you can refer here
You can use the package.json file to get the Express version.
const fs = require('fs');
const package = fs.readFileSync('package.json')
const packageParse = JSON.parse(package)
const expressVersion = 'v' + packageParse.dependencies.express.slice(1)
console.info('Using Express.js ' + expressVersion)
Navigate to the project folder
type the command
npm list express
Note: On wrong path of project fodder it will show empty message.
Using npm list express shows the version and if express is install or not in cmd
below is example:-
C:\Users\Aditya Kumar\nodews\modules_node_js\contact_list>npm list express
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
contact#1.0.0 C:\Users\Aditya Kumar\nodews\modules_node_js\contact_list
└── express#4.18.1
You can refer to https://www.npmjs.com/ official website.
If I understand well ?
Express is a module comes from NPM so you have to follow that command and install the latest express framework there !
$ npm install express --save
CQFD welcome !!

Using NodeJS plugins in Electron

I am new to Electron (Atom-shell), and I am trying to load a NodeJS plugin into the application I am building, but I don't know how. The documentation is not clear on that.
For instance, I am trying to use sqlite3 plugin in my app, I used npm install sqlite3, and it was successfully installed. But the application throws and error when I try to call it var sqlite = require('sqlite3'). Are there any further steps I am not aware of ?
Thanks.
For pure JS (i.e. not native) modules you need the following:
Have the module listed in your package.json dependencies
Let electron know where to find the module (e.g. export NODE_PATH=/PATH/TO/node_module)
The first requirement is obvious and the second has its roots in this issue.
For native node modules (such as sqlite3) which use C++ bindings, you need to build them against electron headers to work. According to electron docs, the easiest way to do that would be:
npm install --save-dev electron-rebuild
# Every time you run npm install, run this
./node_modules/.bin/electron-rebuild
To install the npm modules correctly you should go into the folder of your electron app and install the module via npm.
npm install --save sqlite3
The flag --save is important, because npm will install the module inside your app.
Afterwards the require should work.

ExpressJS installation methods

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

emberjs, node and dependencies

I declare dependencies in my app.js (ember) file, such as:
var _ = require('underscore');
Since my app.js file will be sent to the client and need to execute in a browser, how would this code be made available to the client?
Currently I'm using lineman to concatenate and uglify all my js files, and inside my vendors folder I did include underscore.min.js (all of this get bundled up into app.js) - yet my app isn't working.
Could someone kindly explain the process of adding npm packages to an ember app and the resulting requirements on the client side?
You can use Ember Browserify to use node packages in your ember app.
See this answer for specific usage.
To install Ember Browserify:
npm install --save-dev ember-browserify
and utilize something from a package isntalled through NPM:
import Xyz from 'npm:xyzPackage';

Installing locally express and stylus

I am really new at node js. I was wondering if any one can tell how to install express framework and stylus in a directory which i want. I have installed both globally but cannot find a way to install it locally.A simple suggestion will suffice.
> cd myFolder
create a README.md
> npm init
fill in details
> npm install express stylus --save
You'll then have a express and stylus installed locally and contained in your package.json

Resources