最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the difference between adding Java Script libraries as npm dependencies or simply including them in

programmeradmin5浏览0评论

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?

Share Improve this question asked Mar 23, 2014 at 7:13 diegoaguilardiegoaguilar 8,37616 gold badges86 silver badges133 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 4

On 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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论