npm install not installing things at /usr/bin - node.js

I am trying to install SailsJS with:
$ sudo npm install -g sails
It works, install everything at /home/brunoluiz/npm/lib/node_modules/sails with the following log:
/home/brunoluiz/npm/bin/sails -> /home/brunoluiz/npm/lib/node_modules/sails/bin/sails.js
sails#0.9.16 /home/brunoluiz/npm/lib/node_modules/sails
├── connect-flash#0.1.1
├── pluralize#0.0.5
├── inflection#1.2.5
├── grunt-sails-linker#0.9.5
├── grunt-contrib-clean#0.4.1
├── node-uuid#1.4.0
├── async#0.2.9
├── grunt-contrib-concat#0.3.0
├── grunt-contrib-copy#0.4.1
├── grunt-contrib-coffee#0.7.0
├── ejs-locals#1.0.2
├── ejs#0.8.4
├── underscore.string#2.3.0
├── coffee-script#1.6.2
├── lodash#2.4.1
├── i18n#0.4.1 (debug#0.8.0, sprintf#0.1.3)
├── optimist#0.3.4 (wordwrap#0.0.2)
├── include-all#0.1.2 (underscore.string#2.3.1)
├── sails-disk#0.9.3 (waterline-criteria#0.9.7, lodash#2.3.0, fs-extra#0.8.1)
├── fs-extra#0.5.0 (jsonfile#0.0.1, ncp#0.2.7, mkdirp#0.3.5, rimraf#2.1.4)
├── connect-redis#1.4.5 (debug#0.8.0, redis#0.7.3)
├── grunt-contrib-jst#0.5.0 (lodash#1.0.1, grunt-lib-contrib#0.5.3)
├── glob#3.1.14 (inherits#1.0.0, graceful-fs#1.1.14, minimatch#0.2.14)
├── grunt-contrib-cssmin#0.6.1 (grunt-lib-contrib#0.6.1, clean-css#1.0.12)
├── grunt-cli#0.1.9 (resolve#0.3.1, nopt#1.0.10, findup-sync#0.1.3)
├── grunt-contrib-watch#0.4.4 (gaze#0.3.4, tiny-lr#0.0.4)
├── connect-mongo#0.3.2 (mongodb#1.2.14)
├── grunt-contrib-uglify#0.2.2 (grunt-lib-contrib#0.6.1, uglify-js#2.3.6)
├── waterline#0.9.16 (underscore#1.5.2, q#0.9.7, anchor#0.9.12)
├── grunt#0.4.1 (which#1.0.5, dateformat#1.0.2-1.2.3, eventemitter2#0.4.13, colors#0.6.2, hooker#0.2.3, async#0.1.22, nopt#1.0.10, minimatch#0.2.14, lodash#0.9.2, rimraf#2.0.3, coffee-script#1.3.3, underscore.string#2.2.1, iconv-lite#0.2.11, glob#3.1.21, findup-sync#0.1.3, js-yaml#2.0.5)
├── winston#0.7.1 (cycle#1.0.3, stack-trace#0.0.9, eyes#0.1.8, colors#0.6.2, pkginfo#0.3.0, request#2.16.6)
├── express#3.4.0 (methods#0.0.1, debug#0.8.0, range-parser#0.0.4, cookie-signature#1.0.1, fresh#0.2.0, buffer-crc32#0.2.1, cookie#0.1.0, mkdirp#0.3.5, commander#1.2.0, send#0.1.4, connect#2.9.0)
├── grunt-contrib-less#0.9.0 (grunt-lib-contrib#0.6.1, chalk#0.4.0, less#1.6.3)
└── socket.io#0.9.14 (base64id#0.1.0, policyfile#0.0.4, redis#0.7.3, socket.io-client#0.9.11)
The problem is when you try to use sails: you run sails new project, for example, and it doesn't find sails. Investigating a little bit, I discovered npm is not installing the modules at /usr/bin. Is there some reason for that? Some config?
The same is happening with Karma module.
I used the nodejs from ppa, created the symbolic link for node and installed npm from the official install.sh script (https://www.npmjs.org/install.sh).

It sounds like your npm installation is configured to use /home/brunoluiz/npm as prefix, meaning that it will place symlinks to the CLIs that come with globally installed packages in {prefix}/bin.
In a default installation, prefix is either /usr or /usr/local on Unix platforms (%APPDATA%/npm on Windows).
If {prefix}/bin is not in your $PATH, you won't be able to execute such CLIs just by name.
To see the current prefix value in effect, run:
npm get prefix
Your options are:
Add /home/brunoluiz/npm/bin to your $PATH
Change the value of the prefix configuration item to a folder whose bin subfolder is already in your $PATH; e.g.:
npm set prefix /usr # Ubuntu; CLI symlinks are placed in /usr/bin
npm set prefix /usr/local # OSX; CLIs symlinks are placed in /usr/local/bin
Note, however, that you'd then have to reinstall your global packages for the symlinks to be created in the new {prefix}\bin location.

In addition to mklement0's answer, the first solution (adding the npm binary directory path to the PATH variable) creates another problem when the commands are run with sudo prefix on Linux environment. To solve it, the npm binary directory path must also be added in secure_path in /etc/sudoers.tmp. Here is how:
Why can't sudo find a command after I added it to PATH?

Related

npm install: Is there a way to ignore a particular dependency in package.json

I am currently trying to create a docker container for a node.js project that contains a local dependency. This seems to cause an issue with docker so as a workaround I am trying to just copy the local dependency folders and just ignore their dependency entries in the package.json file. Is there a way to specify dependencies I would like to ignore and have npm install run and skip those enties?
That can be done using devDependencies
The npm modules which you require only to develop, e.g.: unit tests, Coffeescript to Javascript transpilation, minification etc,make the required module a devDependency.
To skip Installation of devDepenencies pass --production flag to npm install,with the --production flag(or NODE_ENV environment variable set to production) npm will not install modules listed in devDependencies."
npm install --production
To make any module to be part of devDependencies pass --dev while installing.
npm install packagename --save-dev
It is a common issue, not only with Docker, but also with some cloud deployments. For example deployment to CloudFoundry using standard Node.js buildpack will cause npm install/yarn to run anyway. So, you'll also need to apply some tricks to work with local modules
If you don't mind to switch from NPM to Yarn for dependency management, you can use workspaces feature.
My package.json looks like this:
{
...
"dependencies": {
"some-module-i-want-to-install": "1.0.0",
"another-module-i-want-to-install": "1.0.0",
"#my/local-dependency-one": "1.0.0",
"#my/local-dependency-two": "1.0.0"
},
"workspaces": ["packages/*"]
}
And my project source layout has the following structure:
.
├── index.js
├── package.json
├── packages
│   ├── local-dependency-one
│   │   ├── index.js
│   │   └── package.json
│   └── local-dependency-two
│      ├── index.js
│      └── package.json
└── yarn.lock
After running yarn, modules I want to install are fetched from NPM registry, and local dependencies are installed from packages directory to node_modules.
.
├── index.js
├── node_modules
│   ├── #my
│   │ ├── local-dependency-one
│   │ │ └── ...
│   │ └── local-dependency-two
│   │ └── ...
│   ├── another-module-i-want-to-install
│   │ └── ...
│   └── some-module-i-want-to-install
│   └── ...
├── package.json
├── packages
│   ├── local-dependency-one
│   │   └── ...
│   └── local-dependency-two
│      └── ...
└── yarn.lock
As you can see, I prefer to define my local packages as scoped (#my/...). It is not mandatory, but a best practice. NPM treats scoped packages as private by default, so I don't need to worry that they will be occasionally published or explicitly mark them as private.

Node/NPM managing local packages

TL;DR How do I configure different "sub-modules" in a modular node.js project to refer to one another as simply as possible?
I'm trying to wrap my head around local packages for NPM, specifically as they relate to a modular project.
I'm building a web app with a front end and a back end API. These need to share a package which exports simple models. My project directory structure looks like this:
package
├── api
│   ├── dist
│   │   └── <compiled files>
│   ├── node_modules
│   │   └── ...
│   ├── package.json
│   └── src
│   └── <source files>
├── application
│   ├── dist
│   │   └── <compiled files>
│   ├── node_modules
│   │   └── ...
│   ├── package.json
│   └── src
│   └── <source files>
└── models
   ├── dist
    │   └── <compiled files>
   ├── node_modules
   │   └── ...
   ├── package.json
   └── src
   └── <source files>
Both the API and application projects are going to use models, so I abstracted that code to a separate sub-module within my project.
I've read the documentation for npm link and that seems to be the right approach because, as I understand it, it symlinks the package in the node_modules dir. This gives access to the code as it exists right now, instead of installing a copy in node_modules. Sounds like what I need, but there is a wrinkle: I'm working on this project from a couple of different places: my laptop, my office at work, and occasionally from home. In addition, others will be contributing to this project in the future.
I would like to make it as simple as possible for a new contributor to get up and running with development.
Currently, a new contributor goes through these steps:
clone the repository
cd into the models dir
run npm install
npm link
cd into the api dir
run npm install
npm link models
cd into the application dir
run npm install
npm link models
start working
What I would like to do (and I think npm should be capable of doing) is:
clone the repository
cd into the models dir
run npm install
cd into the api dir
run npm install
cd into the application dir
run npm install
start working
I could write a script to do this, but it seems like an obvious use case for npm and I suspect that it's probably capable of doing something like this. I think I may be overlooking something because I'm not finding it in the documentation.
There may be better ways to solve this, but..
One possibility is to create a package.json in the root of your project, which handles all of the initialization of your project.
package
├── package.json
And the contents of the package.json has no dependencies, but scripts to link and install dependencies for all of the submodules using just npm install from the package directory.
If you put the following in a package.json in your package folder
{
"name": "package-name",
"version": "1.0.0",
"description": "Some description",
"scripts": {
"init-models": "cd ./models && npm install && npm link",
"init-api": "cd ./api && npm install && npm link",
"init-app": "cd ./application && npm install && npm link",
"postinstall": "npm run init-models && npm run init-api && npm run init-app"
}
}
You will just need to npm install to initialize the whole project.

What are "to" and "update" in the context of npm/node.js?

I am working with node.js/npm.
My current configuration looks like this: npm list -g --depth=0
├── bower#1.7.7
├── cordova#6.0.0
├── grunt-cli#0.1.13
├── grunt-sass#1.1.0
├── ionic#1.7.14
├── ios-deploy#1.8.5
├── ios-sim#5.0.6
├── n#2.1.0
├── to#0.2.9
└── update#0.4.2
As I am newbie in developing mobile apps using Cordova, Ionic, etc. --> what do "to" and "update" stand for?
You're listing top level dependencies for the relative node.js project. to and update are node.js packages that are likely defined in your package.json file and exist in the top level directory of ./node_modules.
To better understand what those two specific packages are, read about them here:
The to package: https://www.npmjs.com/package/to
The update package: https://www.npmjs.com/package/update

Installing Yeoman - "Yo command not found"

I have installed node.js and set up npm on my Yosemite install using the npm-g_nosudo script. When I then run npm install -g yo I see a bunch of content scroll through my terminal window, with no errors, and it completes. I then type yo and OS X tells me that it can't find the command.
Sullys-MacBook-Pro:~ Sully$ npm install -g yo
/Users/Sully/npm/bin/yo -> /Users/Sully/npm/lib/node_modules/yo/lib/cli.js
> yo#1.4.6 postinstall /Users/Sully/npm/lib/node_modules/yo
> yodoctor
Yeoman Doctor
Running sanity checks on your system
✔ Global configuration file is valid
✔ NODE_PATH matches the npm root
✔ No .bowerrc file in home directory
✔ No .yo-rc.json file in home directory
Everything looks all right!
yo#1.4.6 /Users/Sully/npm/lib/node_modules/yo
├── titleize#1.0.0
├── array-uniq#1.0.2
├── figures#1.3.5
├── user-home#1.1.1
├── opn#1.0.2
├── humanize-string#1.0.1 (decamelize#1.0.0)
├── sort-on#1.2.0 (dot-prop#2.0.0)
├── async#0.9.0
├── yeoman-character#1.0.1 (supports-color#1.3.1)
├── string-length#1.0.0 (strip-ansi#2.0.1)
├── cross-spawn#0.2.9 (lru-cache#2.6.2)
├── findup#0.1.5 (commander#2.1.0, colors#0.6.2)
├── chalk#1.0.0 (escape-string-regexp#1.0.3, ansi-styles#2.0.1, supports-color#1.3.1, strip-ansi#2.0.1, has-ansi#1.0.3)
├── yosay#1.0.3 (ansi-regex#1.1.1, ansi-styles#2.0.1, strip-ansi#2.0.1, word-wrap#1.0.3, pad-component#0.0.1, taketalk#1.0.0, minimist#1.1.1)
├── root-check#1.0.0 (downgrade-root#1.1.0, sudo-block#1.2.0)
├── meow#3.1.0 (object-assign#2.0.0, camelcase-keys#1.0.0, minimist#1.1.1, indent-string#1.2.1)
├── package-json#1.1.0 (registry-url#3.0.3)
├── npm-keyword#1.1.1 (registry-url#3.0.3)
├── update-notifier#0.3.2 (is-npm#1.0.0, latest-version#1.0.0, semver-diff#2.0.0)
├── got#2.9.2 (lowercase-keys#1.0.0, timed-out#2.0.0, object-assign#2.0.0, is-stream#1.0.1, prepend-http#1.0.1, nested-error-stacks#1.0.0, statuses#1.2.1, infinity-agent#2.0.3, duplexify#3.3.0, read-all-stream#2.1.2)
├── fullname#1.1.0 (npmconf#2.1.1)
├── yeoman-environment#1.2.5 (log-symbols#1.0.2, escape-string-regexp#1.0.3, untildify#2.0.0, diff#1.4.0, text-table#0.2.0, debug#2.1.3, mem-fs#1.1.0, globby#1.2.0, grouped-queue#0.3.0)
├── configstore#0.3.2 (object-assign#2.0.0, xdg-basedir#1.0.1, osenv#0.1.0, graceful-fs#3.0.6, uuid#2.0.1, mkdirp#0.5.0, js-yaml#3.3.0)
├── insight#0.5.3 (object-assign#2.0.0, lodash.debounce#3.0.3, os-name#1.0.3, tough-cookie#0.12.1, request#2.55.0)
├── lodash#3.8.0
├── yeoman-doctor#1.3.2 (object-values#1.0.0, log-symbols#1.0.2, each-async#1.1.1, twig#0.7.2)
└── inquirer#0.8.3 (ansi-regex#1.1.1, cli-width#1.0.1, through#2.3.7, readline2#0.1.1, rx#2.5.2)
Sullys-MacBook-Pro:~ Sully$ yo
-bash: yo: command not found
I also tried using source .bash_profile and rebooting. The command is still not found. Am I doing something wrong?
This is my .bash_profile
# Add environment variable COCOS_CONSOLE_ROOT for cocos2d-x
export COCOS_CONSOLE_ROOT=/Users/Sully/Development/Libraries/cocos2d-x-3.5/tools/cocos2d-console/bin
export PATH=$COCOS_CONSOLE_ROOT:$PATH
# Add environment variable COCOS_TEMPLATES_ROOT for cocos2d-x
export COCOS_TEMPLATES_ROOT=/Users/Sully/Development/Libraries/cocos2d-x-3.5/templates
export PATH=$COCOS_TEMPLATES_ROOT:$PATH
# NPM
export BIN_ROOT=/usr/local/bin
export PATH=$BIN_ROOT:$PATH
This is my full $PATH
Sullys-MacBook-Pro:~ Sully$ echo $PATH
/usr/local/share/npm/bin:/usr/local/bin:/Users/Sully/Development/Libraries/cocos2d-x-3.5/templates:/Users/Sully/Development/Libraries/cocos2d-x-3.5/tools/cocos2d-console/bin:/Users/Sully/.dnx/runtimes/dnx-mono.1.0.0-beta4/bin:/usr/local/bin:/Users/Sully/Development/Libraries/cocos2d-x-3.5/templates:/Users/Sully/Development/Libraries/cocos2d-x-3.5/tools/cocos2d-console/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
This was resolved by adding export PATH=/Users/UserAccountName/npm/bin:$PATH to my ~/.bash_profile and restarting Terminal.app.
I have faced the same problem after installing everything succesfully, I resolved it using the following method creating symbolic links to yo, grunt, bower like the following :
For Yo
praveen#praveen-lenovo:~$ sudo ln -s /home/praveen/.npm-global/bin/yo /usr/bin/yo
For Grunt
praveen#praveen-lenovo:~$ sudo ln -s /home/praveen/.npm-global/bin/grunt /usr/bin/grunt
For Bower
praveen#praveen-lenovo:~$ sudo ln -s /home/praveen/.npm-global/bin/bower /usr/bin/bower
Here in the above commands the common path /home/praveen/.npm-global/bin/ may change according to your system folder structure.

express is not recognized in cmd in windows 7

js on my windows 7 machine, i have executed the below commands from
C:\node> npm install -g express -generator
and also C:\node> npm install -g express -generator#4, and also the npm link express
for the above commands, i got the below messages.
express#4.10.7 C:\Users\user1\AppData\Roaming\npm\node_modules\express
├── methods#1.1.1
├── finalhandler#0.3.3
├── cookie-signature#1.0.5
├── serve-static#1.7.2
├── merge-descriptors#0.0.2
├── utils-merge#1.0.0
├── range-parser#1.0.2
├── media-typer#0.3.0
├── cookie#0.1.2
├── content-disposition#0.5.0
├── parseurl#1.3.0
├── vary#1.0.0
├── escape-html#1.0.1
├── fresh#0.2.4
├── path-to-regexp#0.1.3
├── depd#1.0.0
├── qs#2.3.3
├── on-finished#2.2.0 (ee-first#1.1.0)
├── etag#1.5.1 (crc#3.2.1)
├── debug#2.1.1 (ms#0.6.2)
├── type-is#1.5.5 (mime-types#2.0.7)
├── accepts#1.1.4 (negotiator#0.4.9, mime-types#2.0.7)
├── send#0.10.1 (destroy#1.0.3, ms#0.6.2, mime#1.2.11, on-finished#2.1.1)
└── proxy-addr#1.0.4 (forwarded#0.1.0, ipaddr.js#0.1.5)
and with npm link express:
C:\node\node_modules\express -> C:\Users\user1\AppData\Roaming\npm\node_modul
es\express
but still when i execute the express nodetest1 at C:/node> i am getting the express is not recognized error.
anyone know any pointers would be helpful.
You have a space between express and -generator above... if this was copy-pasted, maybe you missed it... it's supposed to be express-generator as a single unit.
Make certain that %USERPROFILE%\AppData\Roaming\npm both exists and is in your PATH environment variable... If it isn't there, you will have to open a new prompt after adding it. Some versions of node's installer don't always create or add it correctly.
If you are using [nvm for windows] you will need to ensure that C:\Program Files\nodejs is in your path (the installer should work correctly), you'll need to run nvm via command prompt Run as Administrator. This is because nvm uses symbolic links from the global install path to a specific version under your profile to run against.
I've been using nvm for windows for a few months now (switching from 0.10.x and 0.11.x versions) and it's been a bit easier to work with.
Make sure you are runing the command promt as System Administrator "This is very important", then do npm install -g express after that chang to the directory where you want to create your app and you are good to go. NB if you are using express 4 then install it via npm install -g express-generator#4

Resources