Can not use dependency module right away after installing with --save-dev - node.js

I don't know if this is common. But why I can not use (import or require) dependency modules right away after I installed it with npm install --save-dev module?
Yet I have to reinstall it again using npm install --only=dev and only after that, I'd be able to require the module.
Is this the normal and the most practical way?

You did it the wrong way around. It's: npm install <package name> --save-dev
it can be that if you try to require() it through JS, that it says that the require() method is undefined. But it'll still work if you test your package out.
Hope this helps
Here is an example on how I require my packages:
#!/usr/bin/env node
var awesomevarname = require('packagename');

Related

Do I need to install dependencies one-by-one? (and if so, why?)

I'm porting a node/Express app to Heroku, and it keeps crashing. The log first said, "Error: Cannot find module 'mongoose'" I then (re-)installed mongoose. Then it crashed with "Error: Cannot find module 'moment'"
I require mongoose, moment, etc. in my app. Isn't npm install by itself supposed to take care of the dependencies, or do I additionally have to add all these dependencies, one-by-one, to the package.json file, in addition to npm install? Tks!
In the older version of NPM (5.0.0 or earlier), you had to add --save as a flag to the npm install as:
npm i package-name --save
And it would add it to your package.json. As of 5.0.0 and later, it automatically adds it to the package file.
To answer your question, yes, you will have to add them one at a time for now. And keep in mind what I said before, because if you are using an older version of NPM, make sure you use --save when you are installing so you don't have to do extra work :-)

How to install node_modules only those packages defined in the file package.json

When I used npm install of Laravel project. It creates a lot of unnecessary libraries.
I want to know how to install only the packages I have defined in package.json file.
It is because npm install is recursive. Let's say your project require the module A, which itself require the module B, npm install will end up installing both.
In short, even if you only used one module in your package.json, all the modules you see are needed.
Those packages have dependencies which have dependencies of their own. So unless you want to break your application, I wouldn't alter it.

Module async not found

I'm studying node.js
and I am making the exercises from a book
https://github.com/marcwan/LearningNodeJS/blob/master/Chapter05/05_series.js
I have a problem with an example in which you invoke the module async.js
when I go to run the example I get the error
"can not find module 'async'"
in the folder where you installed node
I checked that there is a module
I also downloaded this package
https://github.com/caolan/async
and launched the test file that works properly
the first question that you do, even if it seems correctly installed the module, there is a command to verify that a module is installed and that you can recall?
the second question is, why is this wrong example?
thanks
To install a package, use npm install package_name.
When that's done, you can easily require that package and use it in your application.
const package = require('package_name');
To install a package globally (so you don't have to install it in every project you create) add -g flag
npm install package_name -g
You should be using npm, not downloading packages from github manually: npm install async.
npm will install the module into a node_modules subdirectory of the directory that you run it in. That directory must be your examples folder, or an ancestor. See: https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

npm install --save, what is the use of not saving

I understand the differences between npm install something and npm install something --save (for anyone wondering, the first one will install the dependency only while the latter will install the dependency and add it to your package.json).
However I do not understand why there is a --save option in the first place. In other words, why would you ever want to install a dependency without adding it to your package.json file? Why is the --save option not default?
A lot of websites/npm modules/SaaS suggest installing their module using npm install something (newrelic is one of them for instance), am I missing something?
Edit: Starting from NPM 5, --save is now on by default.
You would have a scenario such as you need some module to install without adding dependency to package.json file, for ex. you just want to try some module, and not sure you would be really using that module in production or while deploying, so instead adding the module dependency to package.json, just give it a try without using --save. this is why npm install without --save exists.
But For most of your modules you might require using --save, for ex. npm install express --save,
in this case you surely know that you are going to use express for you application.
The other scenario, for not using --save, would be, npm install heapdump or npm install nodemon, I would use it for testing my apps performance, but not add a dependency in the package.json :)
Also, As #surajck said in comment below: when you are doing global installs, in that case adding dependencies using --save, to the package.json would not make sense.
I just learned a nice trick from Jonathan Mills' JavaScript Best Practices course on Pluralsight. From the terminal:
npm config set save=true
Now I don't need to remember --save anymore. And I also now use
npm config set save-exact=true
Because I want the exact version of the package not the ^ prefix.
By default with version npm 5.0+ npm install adds the module to the dependencies list in the package.json file; with earlier versions of npm, you must specify the --save option explicitly. Then, afterwards, running npm install in the app directory will automatically install modules in the dependencies list.

Why would you install node using bower

Ok it may sound like a stupid question.
But I was wondering, why would one install node using bower. I mean bower already requires nodejs and npm as clearly stated on their website, right?
Will try to make it more clear by adding what I added as comment below:
I mean bower already need node and npm, then why would someone use bower to install node again? What's the point? Is there any specific use case? I cant think of any though!
I'm not sure if I understand your question right, but it seems like you are confused on something there.
npm or Node Package Manager it is use for installing/managing the node modules server sides
Usage example: npm install express or npm install bower -g
Whereas bower you use that to install anything that is use client-side
Usage example: bower install jquery or bower install font-awesome
Ignore my above answer if you are talking about why there is a package on the bower component for node.
If so I think they've registered that in bower but not even sure what can you really do with that since most of the stuff would require you to run it server-side, but some library maybe accessible/usable client-side. I have to search/find the example from that to demonstrate but I couldn't find one at the moment.

Resources