Not able to run global modules installed with 'n' - node.js

Have installed Node version manager 'n'.
Installed 'mocha' using
n npm 0.6.4 install mocha -g.
Now when I try to access it using
mocha -R spec
I get an error 'command not found'. Is there some option I'm missing.

'n' is the version manager and not the package manager. In other words, you use 'n' to install different versions of NodeJS. You'll use npm to install packages like mocha.
Type n 0.6.4 will install NodeJS version 0.6.4 and set it in your path.
Type npm install mocha -g will install mocha as global module.
Run both the above commands in order, then you'll find mocha in your path.

Related

All the npm package not installed globally can't be executed directly

Symptom:
I can't execute the npm packages directly that are not installed globally. But I can execute it by npm scripts. How to fix it?
For example:
I installed gulp under the project:
npm install gulp --save-dev
Then I try to execute it by
gulp
zsh: command not found: gulp
But if I add a npm script to package.json:
"scripts": {
"test": "mocha --require intelli-espower-loader && gulp test",
"start": "gulp"
},
Then run
npm start
It can get executed without problem.
P.S.
Same issue with the mocha package, I can't execute mocha directly but I can execute npm test without problem.
Help Wanted:
What I can do to fix that issue?
Notice:
I'm not saying that I want to execute them globally, I just want to execute them under the project.
I don't know where goes wrong, but they are executable not long ago, just don't work recently!
In addition to #Ion's answer: You might need to add the path to the environment variables. In windows OS, it would be %AppData%\npm. For packages installed locally, you should be able to run them like
.\node_modules\.bin\gulp
If you want to execute them globally install with the -g flag
npm install gulp -g
To run directly you could also do ./node_modules/.bin/gulp
That is because the package not installed globally doesn't create a reference in the root node_modules folder . If you want to specifically use the local installed version to run globally on terminal then go to you environment variables and set the path to ./node_modules/gulp/bin/gulp.js or alternatively configure your npm to use the mentioned location as the gulp and execute node_modules/.bin/gulp
You have two choices:
Install the executable modules globally:
npm install -g gulp
This is good if it's a tool you use often (for many different projects). For gulp specifically, the globally installed gulp will also check to see if the project has its own gulp installed (in node_modules) and run that version of gulp. This means that each project can have its own version of gulp. But not all tools have this feature.
Use npx:
npx gulp
Newer versions of npm ships with a command called npx which will search through your project's node_modules directory to find a module to execute. This means you don't need to install project specific tools globally. Apart form avoiding global installs of commands (which may or may not be a security issue) it also allows you to install project specific versions of tools.
If your npm installation does not have npx you can install it from npm itself:
npm install -g npx

Typescript is not removed even after reinstalling node

While working on Angular 5 project, I got the following error on compilation :
#angular/compiler-cli#5.2.11 requires typescript#'>=2.4.2 <2.7.0' but 2.7.2 was found instead.
Using this version can result in undefined behaviour and difficult to debug problems.
Please run the following command to install a compatible version of TypeScript.
npm install typescript#'>=2.4.2 <2.7.0'
To disable this warning run "ng set warnings.typescriptMismatch=false".
To fix this error did the following:
Deleted all node_module instances, and uninstalled node from control panel -> add/remove programs.
Reinstalled node
Checked in cmd:npm view typescript version
I received version as 3.2.2
npm uninstall -g typescript
npm cache verify
Still typescript is not removed from npm.
Can some one help me uninstall typescript completely? Been stuck on this for 2 days now.
It seems there is a small mistake in compiler log:
Please run the following command to install a compatible version of TypeScript.
npm install typescript#'>=2.4.2 <2.7.0'
should be
npm install typescript#">=2.4.2 <2.7.0"
Maybe there is a mismatch in your package json. Update the typescript version as suggested and update package.json
run:
npm install typescript#'>=2.4.2 <2.7.0' --save
Check following step:
You call #angular/compiler-cli in global or local(just one folder), you need install typescript same.
Install typescript match '>=2.4.2 <2.7.0', example : typescript#2.6.2, local or global with -g.
Try close your cmd, reopen it (if typescript install global, cmd don't know it change if you don't reopen).
Check your eviroment variable NODE_PATH, if it exist, go to that folder to remove module in that.
To check NODE_PATH, type in your cmd : echo %NODE_PATH%

npm {package} --save-dev not running at the command line

i tried running
$npm install mocha --save-dev
and then
$mocha
the result is
$-bash: mocha: command not found
If install it globally it works but what if I want to use a package version on this project only
Is there a way to make it works without installing it globally?
I am on a mac 10.11 el capitan
You can use the npx command to run local dependencies
Try the command:
npx mocha
Since you haven't installed mocha globally you have to point terminal to your package's local directory
Run this command instead:
./node_modules/.bin/mocha
this will run the locally installed mocha package.
To use a command installed from an NPM package, you have to install it globally. Try:
$ npm i -g mocha
If you don't want to install it globally, then you can use the local mocha command from:
$ <app>/node_modules/.bin/mocha
$ cd project_directory
If you already have the dependency in package.json file -> do
$ npm install
$ ./node_modules/.bin/<module-name> -V // to get the version
$ ./node_modules/.bin/<module-name> <action> // to use that module
else, if you want to install a new module, firstly do ->
$ npm install <module-name> --save-dev
and then use the commands to get the version and use the module. In this way you will be able to set up and use specific node_modules for a particular project which is a good practice and make your project easy to share.
To install modules globally, run ->
$ npm install -g <module-name>
or with sudo privileges ->
$ sudo npm install -g <module-name>
I hope this helps :) :)

How do I copy global modules between Node installations using nvm?

I manage my Node installations using nvm. I installed the grunt-cli module globally in my previous installation of node. This allowed me to run the command grunt on in the terminal.
When I installed a new version of Node (5.7.1), I got this error whenever I tried to execute the grunt command:
zsh: command not found: grunt
I discovered that the grunt-cli package had not been installed for the new version of Node.
I could install the grunt-cli package again but I would prefer to do this automatically whenever a new version of Node is installed using nvm.
Is there some way to install all the global modules from a previous version of Node when using nvm ?
This can be achieved using the --reinstall-packages-from option when executing nvm install. From the nvm documentation:
If you want to install a new version of Node.js and migrate npm packages from a previous version:
nvm install node --reinstall-packages-from=node
This will first use "nvm version node" to identify the current version you're migrating packages from. Then it resolves the new version to install from the remote server and installs it. Lastly, it runs "nvm reinstall-packages" to reinstall the npm packages from your prior version of Node to the new one.
If your prior version of Node is 4.3.0, the command will be executed thus:
nvm install v5.7.1 --reinstall-packages-from=4.3.0

can't install mmmagic with npm install

I want to install module mmmmagic in my projet using npm install mmmgic, I come across the following error
You either don't have MSVC installed or your build environment isn't set up properly. If you do have MSVC installed, you might try using one of the special VS201x Command Prompts and executing your npm install mmmagic from there instead.

Resources