Looking at npm
starred packages I see that some projects like Grunt, lodash or underscore are avaliable.
I've always used these in the classic way:
<script src="js/lib/lodash.min.js"></script>
What makes it different and how would I use them obtained within the node_modules
packages?
Looking at npm
starred packages I see that some projects like Grunt, lodash or underscore are avaliable.
I've always used these in the classic way:
<script src="js/lib/lodash.min.js"></script>
What makes it different and how would I use them obtained within the node_modules
packages?
- node module packages are intended to be used with application made in nodejs and not other web based applications. As you add reference in .Net project or add jar in your classpath in java application, similarly to use a particular functionality we use npm to add modules to our nodejs application. Hope I cleared your question. – Durgesh Chaudhary Commented Mar 23, 2014 at 7:24
- It cleared and opened my mind. As node.js isn't only about web development and is JavaScript from server side, having such makes sense. – diegoaguilar Commented Mar 23, 2014 at 7:27
- then adding it as answer, please mark the same. thanks. – Durgesh Chaudhary Commented Mar 23, 2014 at 7:28
- Thanks, I think you might answer too, it was you how clearly knew it – diegoaguilar Commented Mar 23, 2014 at 7:29
3 Answers
Reset to default 4On one hand, npm
is a Node tool made to install packages for Node. Packages are collections of modules. And in Node, modules are loaded with a require
call, which is a global function made available by Node.
On the other hand, <script>
is the basic mechanism used in browsers to load JavaScript code.
This may seem mutually exclusive, but npm
can be also used to install packages that are designed to run both in Node and in a browser. In this case we use Node's require
to load a module from the package in Node, but we can use <script>
or Browserify or RequireJS to load the same module in a browser. What method to use in the browser really depends on how the package was designed. You have to read the doc to know or read the source code if the doc is not good. I've designed a npm
package that works this way. You can use Node's require
to load it in Node and use RequireJS to load it in a browser.
npm
can even be used to install packages that are designed to run only in a browser. In this case, npm
is just a convenient delivery and dependency mechanism. I have another package designed this way. It es with a prominent note that it is not made to run in Node. This is an accepted usage of npm
and there are currently proposals (here and here) to make npm
even better at handling this kind of scenario.
node module packages are intended to be used with application made in nodejs and not other web based applications. As you add reference in .Net project or add jar in your classpath in java application, similarly to use a particular functionality we use npm to add modules to our nodejs application.
As node.js isn't only about web development and is JavaScript from server side, having such makes sense.
If you want to add JS dependancies in your html and use a npm like approach you should check out bower.
http://bower.io/
It has the same idea
npm install packageName
except it's
bower install packageName
Also with the --save and --save-dev versions of the install
Bower grabs the latest code from github, and you can specify versions as well.
Instead of package.json you have a bower.json, so if you are given a project with empty dependancies:
bower install
Then bower will go and grab the dependancies also specified versions and boom you are off! You have a bower_ponents directory that holds your entire dependancy tree.
npm for the frontend!