emberjs, node and dependencies - node.js

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';

Related

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

how do I access my module in express?

I install bootstrap with
npm install bootstrap --save
in my express project, then bootstrap is installed in node_modules, but how can I access the js and css in my jade file?
Node_modules is meant to use for server modules. NPM is server package management system.
You should use client package management system like bower for your client side modules i.e. Bootstrap.
Once you set up bower, you'll be able to reference bower_components in your html files.
It depends on how you configure your static server, e.g.:
app.use(express.static('public'));
So if node_modules is placed into public folder, your jade file will be:
script(src='/node_modules/bootstrap/js/file.js')
But better approach is use bower_component instead of node_modules on the client-side

how to load socket.io without npm in node.exe?

How can I include a package like socket.io in node.exe(windows)?
I tried this but it doesn't work:
var socketio = require("./socketio/socket.io")
While my package.json file is stored in a folder called socketio where is in same folder as node.exe is located.
I bundled socket.io via npm inside node_modules. You can download the zip(extract it first) and just use it thanks to node_modules.
You don't want to use NPM, but you do have a package.json(strange)? My advice would be to just install it via NPM. That's what package managers are for!

Configuring a Node JS App to Use NPM

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

Resources