Do I have to learn node.js in order to use npm? - node.js

I need to use a package on npm, so I'm trying to learn how to use npm. The tutorials are fine, but I feel like they're assuming I know node.js, which I don't, and I'm having a hard time finding a tutorial for npm that doesn't also assume I know node.js. Do I need to learn node.js to use npm?

you don't need to know anything about node to use npm, its just a package manager. Install npm and then npm install all the packages you want. You will need to learn the npm toolchain, however, and it also helps to know which options are available for the various commands.
At the very least you should know the difference between installing a package globally and installing a package locally, i.e npm install -g vs. npm install respectively.

Related

npm install required after upgrading node/npm?

I'm trying to understand if it is necessary to run a npm install after upgrading node/npm from 6.x to 8.x. If so, can you please explain why this is done? I had a hard time finding some documentation around this.
You should at least run npm rebuild.
Some packages may choose to do things differently in a different Node.js environment, particularly when it comes to binary modules.

Doesn't npm install check for a global version first?

I just setup a test, and tried to npm install express even though express already exists globally on my system. To my surprise, instead of using the global version, it ended up re-installing a version locally!? Isn't it supposed to use the global version... Or am I suppose to use -g every time, even when I only want to use the existing global version. Otherwise, what's the point of installing anything locally!?
The answer is "NO". It isn't supposed to use your global version.
If you want to use your global version, then you doesn't need to execute npm install at all because it is already installed.
If you do it then, obviously, you are saying "I want to install it locally to my project". And more than that: "I want to install its latest version unless it is declared in my package.json with other explicitly specified version".
In fact, the actual question is: Why in the hell would you want to not install a dependency of your project locally? To have more version mismatch issues?
As #anshuman_singh says, best practice is to always do an npm install --save.
You are able to use globally installed packages, of course. It could be handy for fast testing code that you will drop just after a few hours or so.
But, anyway: If you doesn't have really hard disk or network bandwidth issues, installing all dependencies locally will avoid you too much trouble in the future.
On the other hand, uploading that modules to your code repository is also a bad idea (maybe that is what you were trying to avoid) because, with different versions of node, most native modules won't work if not rebuild. But most VCS support ignoring files and or directories that must not be uploaded.
For example, in git (.gitignore file):
**/node_modules
In summary:
npm init (if you didn't already it).
npm install --save for all your project dependencies.
npm install --save-dev for dependencies not needed in production (testing stuff).
Don't upload node_modules to your VCS.
After new checkout: npm install or npm install --production (to not install dev-dependencies).
npm install -g only for tools you will use in console.
This way, you are sure that you will have in production (or other dev environments) the exact same version of each package.
And, finally, if you ever want to upgrade some package to its latest version, simply run:
npm install --save <pagkage_name>#latest.
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.
The first option is the best in my opinion. Simple, clear, explicit. The second is really handy if you are going to re-use the same library in a bunch of different projects
Install locally-
npm install moduleName
install locally and save in package.json-
npm install moduleName --save
install globally-
npm install moduleName -g

npm path and installation issues - suggestions

I have used to install my nodejs on D:\ drive instead of C and have set environment variables to D drive node & npm folders.
Then i changed npm installation path as "prefix=D:\node\node_modules\npm
" on "npmrc" file. So i could confirm that all user based modules are pointing on D drive npm folder instead of appdata.
I tried to install express js globally and i used to check the package tree on my cli as mentioned below,
npm ll -g
while trying this command am getting npm extraneous ERR,
Please suggest me that which way i have to use npm path and installation stuffs.
Thanks in advance.
It might seem like a good idea to install packages globally, but this is one great reason not to.
Often used packages like express, and cookies should be kept local to a package. Mostly because of versioning issues. You might have one package using express2, but your new one wants to use express3. You would have trouble if it was a global install. When in doubt leave off that -g, and use a --save instead. (This adds the package to your npm dependencies list.)
On the other hand, command line tools like mocha, yeoman, and uh not much else that I know of should be installed with the -g flag.
I'm not much of a windows person, so you'll have to look a little yourself, but I would also recommend not installing Node by hand, but instead using a version manager like nvm to do that stuff. Here's an nvm port for windows: https://github.com/coreybutler/nvm-windows

Confused in starting a project in node.js with npm install

Hello I am just a noob and still learning. I have already downloaded and tried the chat tutorial of get-started part from socket.io. Now, I am again learning from another source. What's confusing me is that, do I always have to npm install in the beginning of every project after writing the dependencies in the package.json? Or is there any other way? I would be very glad if you could help me understand my confusion. Thank you!
Yes, before running, all dependencies must be installed. So you must run npm install.
When developing, you can use npm install --save <package_name> to install a dependency and automatically add it to package.json.
NPM means Node Package Manager. It is used to manage your dependencies to other node modules dynamically thanks to a configuration file called package.json. This way you can easily define the exact versions you need or a mask in order to always retrieve the stable ones for instance.
The command npm install allows to interpret your configuration file and then download the good versions (and this recursively).

Additional installation steps with npm install

I can't find this documented anywhere, but I'm working on a node package that will be installed via npm install -g. This includes a binary and also several other sub packages that will be yeoman generators. As far as I can tell, these also have to be installed via npm install -g (or npm link). There doesn't seem to be any other way to use a generator. If there is, it would be fine to point yeoman to one of the generators relative to this project too.
When my package is installed, I want all of the generators to be installed as well since my script will ultimately end up calling yo one-of-my-generators.
Is there anything I can do to make npm install of my project also install/link all of the generators it contains in one step?
add them as peerDependencies in your package.json file. See the blog at http://blog.nodejs.org/2013/02/07/peer-dependencies/
Its also how we install things like mocha

Resources