LEARNING JAVASCRIPT - Trang 47

$ npm install --save underscore
npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
[email protected] node_modules\underscore

You might be wondering what all of these warnings are. npm is telling you that there

are some components missing from your package. For the purposes of this book, you

can ignore these warnings: you only need to worry about them if you’re using npm to

publish your own packages, which is beyond the scope of this book.
Now if you look at your package.json file, you’ll see that Underscore is listed as a

dependency. The idea of dependency management is that the dependency versions

referenced in package.json are all that’s necessary to re-create (download and install)

the dependencies themselves. Let’s try this out. Delete the node_modules directory

again, and then run

npm install

(note we don’t specify any particular package

name). npm will install any packages listed in the package.json file. You can look at

the newly created node_modules directory to verify this.

Build Tools: Gulp and Grunt

For most development, you’ll probably want a build tool, which automates the repeti‐

tive tasks you perform as part of the development process. Currently, the two most

popular build tools for JavaScript are

Grunt

and

Gulp

. These are both capable build

systems. Grunt has been around a couple of years longer than Gulp, so the commu‐

nity is larger, but Gulp is catching up fast. Because Gulp seems to be the increasingly

popular choice for new JavaScript programmers, we’ll use it in this book, though I am

not prepared to say Gulp is superior to Grunt (or vice versa).
First, you’ll install Gulp globally with:

$ npm install -g gulp

If you’re on Linux or OS X, you’ll need elevated privileges to use

the -g (global) switch when running npm: sudo install -g gulp.

You’ll be prompted for your password and given superuser privi‐

leges (for that command only). If you are on a system that someone

else manages, you might have to ask them to put you in the sudoers

file.

You’ll only need to install Gulp globally once for each system you develop on. Then,

for each project, you’ll need a local Gulp, so from your project root, run

npm install

--save-dev gulp

(Gulp is an example of a dev dependency: your app won’t need it to

run, but you’ll use it to help with your development process). Now that Gulp has been

installed, we create a

Gulpfile (gulpfile.js):

const

gulp

=

require

(

'gulp'

);

// Gulp dependencies go here

ES6 Features | 23

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.