NPM won't automatically set up cmdlet's for me? - linux

this is the first this ever happened to me.
I am trying to install a documentation generator (docma) and for some reason I follow everything they say in the docs and can't execute 'docma' or 'docma serve', it says it's not recognized as a cmdlet, tried it on my Linux machine and it also isn't recognized as a command, so there's no way for me to serve my documentation and the developer stopped supporting that years ago.
Thanks in advance
EDIT: I should add that I don't get a node_modules folder

If you don't have a package.json file for your project, create one:
npm init
(If you don't care about what the contents of the package.json file are, then you can accept all the defaults with npm init -y rather than answer the questions the command will ask you.)
The documentation says to install docma in a project like this:
npm i docma -D
Do that. That will create a node_modules directory if you don't already have one.
From there, all the commands in the docma documentation can be executed from the command line if you precede them with npx. So where it says docma, you can use npx docma. And where it says docma serve, you can use npx docma serve. (You will need to be in your project directory for this to work!)
Another solution would be to install docma globally (npm i docma -g) but that has its own risks and complications (such as requiring elevated privileges in some situations). What I've described above is (IMO) safer and better.

Related

Node JS/ Angular

I have installed Node JS and then imported an existing work project in Visual Studio Code. After that I ran npm install in the project folder, then run npm start and the app comes up fine. However, if I run an ng command I get an error telling me "ng is not valid command", even though the Angular CLI is in the modules folder.
I tried manually installing Angular CLI globally and set system path to point to the npm folder, and then the ng command works fine. What I don't understand is why do I need to install CLI globally if I just want to run that command within the project where the module is already present?
As a general rule then you will need to install globally any commands you wish to use (without NPX). This isn't really a restriction of NPM so much as it is a fundamental way in which command line programs work. The OS will only look in fixed predefined locations set in PATH. This applies to any Node based tool such as grunt or ng or whatever.
(While some systems do look for executables relative to the current working directory, or can be configured to, it's generally not a good or reliable method and NPM doesn't rely on this behaviour).
For something like the Angular CLI then installing it globally should be fine and is what many people will do. As a general rule if it is a command you want to run, rather than a dependency for a project, you can consider installing it globally. You'll notice that on the Angular CLI page the example does exactly that.
In many cases however you might want to run a command from a local project. Perhaps for a build script or something else where you want to keep it isolated. In that case you instead prefix your command with npx which will look inside the local project for commands.

node user directory leading to command not found

I suipidly ran this script to stop having to use sudo on npm -g commands and now my node_modules are located at:
/Users/myusername/.npm-packages/lib/node_modules
when trying to run a commands i.e yo bower i get -bash: bower: command not found
Do I need to link this directory to the usr/bin?
In trying to sort this out I have run lots of commands and created symbolic links, I feel like my system is a real mess and I cannot really see what is going on? Help to ensure my system is clean would be really helpful.
Generally, it's a good idea to ensure that you do not need admin rights to run npm commands. The problem you're having, is that the directory where npm now installs its commmands (/Users/myusername/.npm-packges/bin) is not in your PATH. Your PATH is a list of directories where your shell searches for the commands you type.
You can add the directory to your path by adding the following line to the .bashrc file in your home directory (create it if it doesn't exist).
export PATH="${PATH}:/Users/myusername/.npm-packages/bin"
Open a new terminal, and bower should work again.
As an additional tip, I believe you're running on OS X? When installing Node.js using Homebrew, it is automatically installed in a proper way (no sudo needed to install packages globally and they will just work). Next time you're installing Node.js (or something else), Homebrew might be worth a try.

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

yeoman permission issue with nodejs and angular

I am trying to start an angular web app with yeoman but I get permission issue when trying to install the new generator. I can bypass and install generator with sudo but then I get the permission errors when running
**yo** angular
I deduce its because its trying to access npm modules that are global which the current user doesnt have permissions for, and I cant run Yo with sudo. I have done a lot of google searches and they all involve some type of hack with the NODE_PATH in the .bashrc or moving the node modules to the home directory. Has anyone found a simple solution for this issue.
Below is my problem in screenshots:
yo installs fine
when i try to install the generator without sudo complains..
install with sudo passes.
then finally when I try to run yo angular it breaks.. I believe its because yo runs as user and I have installed everything with sudo privileges. How can I get past this?
The reason it breaks, I guess, is because the whole directory tree was created as super-user.
The hacks you mentioned about using NODE_PATH and the home directory are not hacks. They exist for this same very reason. To tell node where to look for packages. And .bashrc is the place where you are supposed to change this kind shell variables.
Say you added ~/.node_modules to you NODE_PATH, then you can install all "global" in there. You could also change the permissions on /usr/local. But on linux world that is not recommended.
I also strongly recommend in not using global install with npm. Using -g and npm link is handy when developing but you shouldn't count on them. You can introduce subtle bugs in your code when you forget to add a package on you package.json but it is installed globally.
Instead of installing it globally, you can find all the packages executable on ./node_modules/.bin/ directory.
But lets say you don't want to be typing ./node_modules/.bin/yo all the time, you could create an alias on your .bashrc.
alias yo="$PWD/node_modules/.bin/yo"
and it would work like expected, and if there is no yo package installed, you get an error.

The directory to use npm to install package

I just get started on nodejs. I have installed nodejs and npm. Now, I want to install some packages like mongodb and express. As my default directory path in cmd is C:\>Users\administrator, do I need to make current folder as nodejs folder to run npm install express/coffee-script or I can just run this command under the default directory path mentioned above?
By the way, I always see the npm install command provided by others starts with a dollar sign, but I can only use the command without the dollar sign. So what does the dollar sign stand for?
By default, npm will run in local mode, and install scripts into ./node_modules. This is great if you need to require your scripts, as you'll do with Express.
Calling it with the -g option installs it globally, wherever node is installed (usually, on Linux, in /usr/local. This is great for packages that are meant to be run using the shell (for example, Supervisor).
Generally, if you want to develop a node.js application under C:\foo\bar\myapp, you will run npm from there.
FYI, the $ sign is a general indication meaning that the following command is meant to be run on the command line.

Resources