Tag: npm


Automate your development tasks using Grunt

Grunt is an extremely useful Node.js package to automate lots of development and continuous integration tasks. The Grunt eco-system has lots of packages available on npm. This enables us to quickly setup our development/continuous integration environment. Grunt tasks mostly have two required properties. An files array, which is used to configure on what files the tasks is executed, and an options property which configures some task specific settings. The files array supports the globbing and minimatch pattern to match files based on the provided expression. So what tasks could you use for your projects, or for which project can you use Grunt? How do I configure Grunt tasks? How do I execute them? All these questions I try to answer for you in this article.


npm tips and tricks

In my previous post I showed you how easily you can create a simple webserver using Node.js. In this post I want to show you how to make more advanced usage of node package manager. npm initUsing node package manager you can get an even quicker start of your project by using the npm init command. So let’s get started by opening a command prompt (on windows open the Node.js command prompt). Then create a new folder and navigate into this newly created folder. In the folder execute following command and answer the questions or press enter for the defaults. 1234567891011121314151617181920212223242526272829303132333435363738394041424344Your environment has been set up for using Node.js 0.10.17 (x64) and npm.Press any key to continue . . .C:\Users\Marco> mkdir NodeJsPackageExampleC:\Users\Marco> cd NodeJsPackageExampleC:\Users\Marco\NodeJsPackageExample> npm initThis utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sane defaults.See `npm help json` for definitive documentation on these fieldsand exactly what they do.Use `npm install --save` afterwards to install a package andsave it as a dependency in the package.json file.Press ^C at any time to quit.name: (NodeJsPackageExample) node-js-package-exampleversion: (0.0.0) 0.0.1description: Total package awesomenessentry point: (index.js)test command:git repository:keywords: package, awesomenessauthor: Marco Franssenlicense: (BSD-2-Clause) MITAbout to write to C:\Users\Marco\NodeJsPackageExample\package.json:{ "name": "node-js-package-example", "version": "0.0.1", "description": "Total package awesomeness", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "package", "awesomeness" ], "author": "Marco Franssen", "license": "MIT"}Is this ok? (yes) yes After answering the questions the result should be something like above. As you can see I have changed the default name. By default node package manager uses the folder name. Play around yourself to get the desired result. You can also change the package.json after it is created. Why do we need this package.json file?


Starting with a Node.js webserver

Before starting to explain how you start your first Node.js project for building a simple web server I will first explain you what Node.js is. To do so I just include a quote of the Node.js themself, because I don’t like to reinvent the wheel. Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. The next thing you should know is about the Node.js Package Manager (NPM). Using NPM you can benefit from the thousands of open source packages so you won’t have to build everything yourself. I suppose you already have installed Node.js. Let’s start with a simple Hello World in Node.js by creating a file called hello-world.js and execute our script to show the result. hello-world.js1console.log('Hello World'); We can execute the script using following command. (On Windows open the Node.js command prompt) Navigate to your folder containing the just created hello-world.js file and execute the script using node. 123456Your environment has been set up for using Node.js 4.1.1 (x64) and npm.Press any key to continue . . .C:\Users\Marco> cd MyHelloWorldCodeFolderC:\Users\Marco\MyHelloWorldCodeFolder> node hello-world.jsHello World