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.
Related
This is quite basic. Many examples show terminal commands such as: 'express name-of-directory-to-be-created'. Yet on my system, though I've installed node.js (and command: node -v works, and npm -v works), command: express fails with: command not found Further investigation shows that, on my macbook air, all incidences of express.js are not executable. I believe express was installed using npm express or npm -g express. Please, what is going on? Are the examples correct? ( There are several, all from various sources. ) How may I execute
express directory
Thanks in advance.
This usually happens on windows.
You need to install express globally and the location where npm modules get installed should be added to PATH(environment variable).
To install express globally type npm install -g express
Also it looks like you are trying to use express-generator.If yes you should install it similarly.
npm install -g express-generator
In windows by default npm modules are installed at "C:\Users\u_name\AppData\Roaming\npm"
Edit your PATH environment variable and append your npm path at the end.
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.:
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
I have nodejs installed on a MBP which runs OSX 10.9, I have installed as a package downloaded from the nodejs website. Then I have installed the MEAN stack following instructions on mean.io.
The commands are:
sudo npm install -g mean-cli
mean init yourNewApp
That works correctly
Now the real issue is when after my app is created I enter the dir using the terminal, and write gulp, and it thows me some errors that some mandatory modules are not found.
The modules are written in the package.json file that mean generated, and they are installed as global modules on ~/.npm
I browsed the folder and there are all the required packages folders, inside the folders there is a package.tgz file which has the code of the package and a package folder which holds a package.json file describing the package itself.
Now I don't understand why the packages are compressed and why if they are installed globaly can not be accessed from gulp on my project folder.
Thanks in advance.
If you install some global module then you better don't put it into the package.json of your app because when you run your app that's the first place where is going to search and if it is there your app is going to look at node_modules folder and if it is not there your app will crash.
My advice is try to install your modules inside your app, npm install your_module --save because your app is gonna be portable and with a simple npm install you will be able to install all your needed packages.
But if you still wanna install global packages you maybe wanna follow this rules:
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.
If you have the time to read the link then you will see that there are exceptions and how to handle them.
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.