Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use npm for front-end dependencies?

I want to ask if it is possible (and generally a good idea) to use npm to handle front-end dependencies (Backbone, jQuery).

I have found that Backbone, jQuery and so on are all available through npm but I would have to set another extraction point (the default is node_modules) or symlink or something else...

Has somebody done this before?

Is it possible?

What do I have to change in package.json?

like image 397
dev.pus Avatar asked Jul 07 '12 05:07

dev.pus


People also ask

Can I use npm for frontend?

Yes, you can use npm to install basically anything that you need on the front-end, including CSS-only packages. Let's use a popular example.

How do I use npm dependencies?

To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install ) or the --save-dev flag for devDependencies.

Can I use node for frontend?

A common misconception among developers is that Node. js is a backend framework and is only used for building servers. This isn't true: Node. js can be used both on the frontend and the backend.

Does npm build Dev dependencies?

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.


Video Answer


2 Answers

+1 for using Browserify. We use it here at diy.org and love it. The best introduction and reasoning behind Browserify can be found in the Browserify Handbook. Topics like CommonJS & AMD solutions, build pipelines and testing are covered there.

The main reason Browserify works so well is it transparently works w/ NPM. As long as a module can be required it can be Browserified (though not all modules are made to work in the browser).

Basics:

npm install jquery-browserify

main.js

var $ = require('jquery-browserify');
$("img[attr$='png']").hide();

Then run:

browserify main.js > bundle.js

Then include bundle.js in your HTML doc and the code in main.js will execute.

like image 81
Derek Reynolds Avatar answered Oct 12 '22 15:10

Derek Reynolds


Short answer: sort of.

It is largely up to the module author to support this, but it isn't common. Socket.io is an example of such a supporting module, as demonstrated on their landing page. There are other solutions however. These are the two I actually know anything about:

  • http://ender.no.de/ - Ender JS, self-described NPM analogue for client modules. A bit too involved for my tastes.
  • https://github.com/substack/node-browserify - Browserify, a utility that will walk your dependencies and allow you to output a single script by emulating the node.js module pattern. You can use a jake|cake|rake|make build script to spit out your application.js, and even automate it if you want to get fancy. I used this briefly, but decided it was a bit clunky, and became annoying to debug. Also, not all dual-environment npm modules like to be run through browserify.

Personally, I am currently opting for using RequireJS ( http://requirejs.org/ ) and manually managing my modules, similar to how Mozilla does with their BrowserQuest sample application ( https://github.com/mozilla/BrowserQuest ). Note that this comes with the challenge of having to potentially shim modules like backbone or underscore which removed support for AMD style module loaders. You can find an example of what is involved in shimming here: http://tbranyen.com/post/amdrequirejs-shim-plugin-for-loading-incompatible-javascript

Really it seems like it is going to hurt no matter what, which is why native module support is such a hot topic.

like image 17
Steve McGuire Avatar answered Oct 12 '22 16:10

Steve McGuire