How does npm package generate cmd line file - node.js

When we install Bower or Grunt via npm, we can use the bower or grunt command line tool. Looking inside, it seems to have generated a .cmd file for Windows and (probably, because I haven't looked into it) a .bach file for Unix based system.
I looked at the source code in Github, but haven't got a clue. How does those npm projects generate the cmd batch file which supports both kind of OSes?

Are you looking for this repo ?
https://github.com/ForbesLindesay/cmd-shim

Related

What is the 'npm' command and how can I use it?

What is npm?
Whenever I browse through some project they ask me to run npm command, something like this
npm install -g node-windows
I went through some blog posts to learn about npm and I installed Node.js. However, when I run the above code in Node.js, I get the following errors:
When I browsed further, I came to know that the windows user can run the command from the cmd prompt window, but when I do that I get some output like this:
Which just generate a text file nothing else.
My questions:
How can I get started with the AngularJS2?
How can I run an npm command?
Do I require a command prompt to run the npm command (in Windows), or I can just use Node.js?
When I use the command npm install in my command prompt, I get the following output:
How to get started with the angularJS2
Follow this link and set up the project by following instructions
How to run a npm command
npm stands for Node Package Manager, and therefore you need Node.js installed before you can run npm commands.
Follow this and install the latest version. And restart the command prompt.
Do I require a command prompt to run npm commands (in Windows), or can I just use Node.js?
Yes, you need to run npm commands from the command line (in Windows).
E.g., npm install
You get the warning because there is no package.json file present where you are running the command.
ENOENT stands for Error NO ENTrey
Navigate to the project folder using the following command and then run npm install
cd <projectpath>
The AngularJS 2 website has everything you need to be covered. Their quickstart guide alongside with the quickseed zip file helps a lot.
But, in case you missed some points:
yes, you will need npm/NodeJS. So, download the latest distribution and have a clean installation of it.
you can execute the npm command with its parameters from within the Windows cmd.
the quickseed ZIP file contains all the files you need to see a live and quick example running locally. Unzip it on your workplace and navigate to it using the windows cmd. When inside the root folder of the unzipped package, execute npm install and right after it npm start.
Take the learning path. Step by step, all your questions will be answered.
You need to use an admin prompt for global installation (-g).

Node js command line doesn't work on ubuntu

I've built a command line using Nodejs, I was developing it on windows, than after I published it on npm, I installed it from Ubuntu, then I tried to use it, it says that there is no such file.
I'm not sure of what is the problem, I have doubt that it's from this line :
#!/usr/bin/env node
I also tried to clone the repo from ubuntu, and I run the command
npm link
and it worked ! can someone please tell me what did I do wrong ?
Here is the whole repo on github and the package on npm

Bower, Grunt on Ubuntu 12.04 - command not found

I've installed bower and grunt on my machine but non of it works. I get :command not found for both.
I've placed paths to bower and grunt in .bash_profile file, like:
export PATH="/home/user/.node/lib/node_modules/grunt-cli/bin:$PATH"
export PATH="/home/user/.node/lib/node_modules/bower/bin:$PATH"
It feels like packages are installed correctly but it can't be found.
Npm and node is located in home/user/.node and home/user/.npm directories is this is the right place for it?
which bower/grunt outputs nothing
Just had to remind myself of this one, to set up environment on a new machine.
As per http://gruntjs.com/getting-started, there are two steps required for installation and use of Grunt.js task runner on a given project:
You should globally install only 'grunt-cli', the Grunt Command Line Interface. This will put the grunt command on your system path. This is achieved by running npm install -g grunt-cli, which may require root privileges depending on your setup.
You should locally install the grunt task runner proper. This is achieved by running npm install, after adding the desired version of Grunt.js to your project's package.json file. This will install the specific version of Grunt.js described in your project's package.json, under the devDependencies section. This is the file used by nodejs to describe project development and deployment dependencies, among other stuff.
I managed to fix it by adding paths to .bashrc file, like:
PATH=$PATH:/home/user/.node/lib/node_modules/grunt-cli/bin
PATH=$PATH:/home/user/.node/lib/node_modules/bower
Reference

NPM throws errors at traceur install on OS X

https://github.com/tejas-manohar/itnerary-civic-hacking -- I cloned this git repo down locally. Installed grunt-cli globally, and ran npm run nss (script written by author of original server template -- look in package.json, not complex) and faced numerous errors at the traceur install + git cloning. I've included the trail from terminal window in the pastebin linked below. The npm debug/error log mentioned is not present at the suggested location. npm install alone does not help the situation and returns nothing back. NodeJS v0.10.29 is installed via Node Version Manager (NVM) on OS X 10.9.3.
http://pastebin.com/UJFL3k2E
I'm not the most adept with some of the technologies discussed here. Would someone please attempt to walk me through (a) solution(s) and/or steps to get better clues? All assistance is appreciated.
There's a couple of problems going on which are causing the problems you have seen. The first of which is the command that is run when you run npm run nss, which fails for you with this:
rm: ../../app/static/js/vendor/traceur.js: No such file or directory
This is because, well, the file simply does not exist (yet). If you look at the commands which are run when you run npm run nss, you'll see that the remove file is followed by the copy file of traceur.js to that very location. So I would guess that whoever wrote the script intended for this to be run after it was initially setup, and the remove/copy would be done once the file was initially copied there. Therefore, you must first copy the traceur.js file to app/static/js/vendor before you can successfully execute npm run nss.
But then the next problem, the file should be copied from tools/traceur-compiler/bin doesn't exist either. This is because the traceur-compiler project has recently deleted this (compiled) file from their project, which you can see via this commit: https://github.com/google/traceur-compiler/commit/429c3d850dcb7636320e81fd782c61a06de0fbf1
So you need to regenerate this file, which you can do by (from the itnerary-civic-hacking root directory):
cd tools/traceur-compiler
make bin/traceur.js
cp bin/traceur.js ../../app/static/js/vendor
This will get you in the same state as if you successfully ran npm run nss.
You won't be able to re-run the npm run nss command, but you really shouldn't need to since its more of a setup command than anything (it creates a directory, clones a github repo, etc). If you wanted to update the traceur-compiler in the future (though you may never have to do this) then I would recommend running the following steps (from the itnerary-civic-hacking root directory):
cd tools/traceur-compiler
git pull origin master
npm install
make bin/traceur.js
cp bin/traceur.js ../../app/static/js/vendor

bower command not found windows

I am having huge problems trying to use bower (to install foundation 5) or get anything bower related to work on the command line.
I've installed
ruby 1.9.3
git (with run from command prompt option)
Nodejs
I've successfully ran
npm install -g bower grunt-cli
gem install foundation
and these have ran fine. I've looked in the npm/node_modules directory and I can see bower folder in there.
Every time I type bower either into the cmd, ruby cmd or git bash I get
command not recognized
I've set the git path in the environment variables.
What's annoying is the instruction on the bower site. It states:
npm install -g bower
bower install
Well this doesn't work!
I bumped into this problem after npm install -g bower too. I solved the problem by adding npm's binary folder to my path.
Here are some helpful hints for doing that:
Find the location of your npm global binaries: npm config get prefix. This path may look something like C:\Users\username\AppData\Roaming\npm (or C:\ProgramData\chocolatey\lib\nodejs.commandline.X.XX.XX\tools if you use Chocolatey).
Add the path from step 1 to your Path.
Open the Windows Control Panel, search for environment, then click on either edit environment variables for your account, or Edit the system environment variables`.
Find the variable named Path or PATH, or create one if it doesn't exist.
Paste the path from step 1 here (; delimited).
You may need to restart your command prompt window.
You should now be able to enter bower commands.
in case this helps.
I'm a npm / bower nooB - but what happened in my case was, that I was using the Angular JS tutorial,
which seems to have bower set up to be used via npm, and NOT via the command line directly.
Note: in my case I think my bower install got messed up.
I ran this to fix up my bower install:
npm install -gf bower
Then I edited my bower.json file to add in a new library that I wanted to use (in my case angular-sanitize)
I CD to the location of my project
cd myProjectPath
Then to run bower, I actually used npm install:
npm install
This seems to to run bower as a node package, which in turn scans bower.json and installs any missing bower packages.
To run bower as a npm package, add npm before the bower command:
npm bower -v
hope this helps,
Sean
If above solutions don't work. I think you should specify the absolute path to use the bower in command prompt. In my app folder, I just call
C:\Users\yourusername\AppData\Roaming\npm\bower.cmd install
This is content in my bower.cmd. It looks like my windows can't recognize the ~dp0 variable,
node "C:\Users\yourusername\AppData\Roaming\npm\node_modules\bower\bin\bower" %*
This definitly will happen if your Windows PATH variable close to 1024 symbol length. So new links such "C:\Users\username\AppData\Roaming\npm" simply won't fit into that.
Check your PATH, remove extra stuff and try to reinstall node.js and bower etc. Also there is a way to increase 1024-length limit
As others mentioned have to add bower into environment variables, but the easiest way to locate is just simply type in your npm location first, then use Browse File and locate bower on your system(you can search for it in search bar as well), because the location may vary for different users.
1. Type in windows cmd:
for /f %a ('npm config get prefix') do setx path %a
2. Reload cmd
Same issue. I solved it by using another version of bower. It works when i tried to use version 1.2.6
$ npm install -g bower#1.2.6
I had this problem after installing Chocolatey 0.9.8.28. I tried most of the solutions listed here, but I was unable to have 'bower -v' recognized in the Powershell commandline. This seemed a little strange since the Chocolatey Install is 'all about' Powershell. Eventually I was able to succeed by setting the path so that Powershell could find the updated PATH variable settings, but I needed help to understand why and how to do it, which I am providing below:
After failing with Powershell initially, I was able to go to a standard DOS Commandline (not by using cmd /c in powershell) and successfully perform all the variations of path setting that are discussed above and have success in seeing bower and the bower commands recognized by the DOS command line. And the cmd window showed the full updated path (C:\ProgramData\chocolatey\lib\nodejs.commandline.0.10.34\tools) - notice that the version number may change from the listing by the honorable KimchiMan.
This PATH persisted through closing and opening new DOS command line windows, but it was not recognized (ever) in any Powershell windows.
Then I discovered the following related discussion: Set Powershell Path Settings.
This points out that Powershell recognizes the HKLM path settings version when starting up. AND it is possible to set the Path EITHER IN the session (non-persistent) or in the Registry (persistent). The following is directly from that discussion.
These commmands can set the PATH temporarily for a powershell SESSION:
$env:Path = 'New path'
[System.Environment]::SetEnvironmentVariable('Path', 'New Path', 'Process')
These commands can set the PATH permanently for all new Powershell Sessions:
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name 'Path' -Value 'New Path'
[System.Environment]::SetEnvironmentVariable('Path', 'New Path', 'Machine')
But I've messed with setting registry settings programmatically before and I don't believe a line or two of PS script is at all up to the task. So I did not even try this option.
Instead I went to the Control Panel/System console and changed the path through the Environment Variables dialog. I noticed that all the effort I had made to change the path with set PATH in a DOS commandline had worked during the windows session, but the new path entry had not been persisted to the Environment Variables/Registry even though the Dos session was running as administrator.
After I made this change through the Control Panel, the updated path for bower in Chococlatey's tools folder (and presumably all subsequent tool installs) were permanently set and bower was available in both the DOS command line and in the Powershell command line.
hope this helps...
Uninstalled 64bit and Installing 32-bit version of nodejs along with git-
32bit worked!
Had the same issue.
I just ran "npm install -g bower" then visited the directory where it was downloaded, in my case the location was "C:\Users\user\AppData\Roaming\npm\node_modules\bower"
From the directory I ran bower, and everything working fine, even from other directory

Resources