Tạo project Node.js với npm (command line, cmd, terminal)

Tạo project Node.js với npm (command line, cmd, terminal).

(Xem lại: cài đặt, cấu hình nodejs, npm)

Ở các ví dụ trước, bạn chỉ cần tạo file js ví dụ index.js sau đấy dùng lệnh node index.js để chạy

Đó là trường hợp đơn giản, còn với trường hợp phức tạp thì bạn cần phải tạo 1 project Node.js theo chuẩn để thực hiện mô tả, quản lý module, script…

Tạo Node.js project bằng npm

Để tạo project Node.js bằng npm trên giao diện dòng lệnh bạn phải cài Node.js và npm trước đó.

Muốn tạo project ở thư mực nào thì ta mở giao diện dòng lệnh ở thư mục đó và chạy lệnh npm init

Ví dụ mình tạo một project Node.js ở thư mục C:\nodejs\demo1

Sau khi chạy lệnh npm init ta tiến hành nhập các thông tin cho project như tên, version, description.

  • phần entry point sẽ là file main (file js được chạy đầu tiên). Mặc định là file index.js
  • test command là lệnh được dùng để test. (ở đây mình để lệnh test là node test.js)

Tạo project Node.js với npm (command line, cmd, terminal)

Sau khi hoàn thành nó sẽ sinh ra file package.json chứa các thông tin của project

Phần scripts sẽ chứa các lệnh chạy từ npm, ví dụ chạy npm test tương ứng với node test.js

Các bạn có thể thêm các script khác tại đây, ví dụ mình muốn start bằng lệnh npm nên mình thêm script start vào trường “scripts”:

"scripts": {
  "test": "node test.js",
"start": "node index.js"
}

Chạy Node.js project bằng npm

Tiếp theo bạn tạo file index.jstest.js trong cùng folder với file package.json

console.log('hello world')
console.log('test')

Bây giờ start project bằng lệnh npm start

Tạo Node.js project bằng npm

Hoặc chạy test với lệnh npm test

chạy node.js bằng npm

Quản lý module project với npm

Trường hợp bạn muốn dùng thêm các module thì có thể khai báo nó trong trường dependencies rồi sau đấy chạy lệnh npm để nó tự động tải về cho project (Cái này rất giống với file pom.xml của maven trong Java)

Ví dụ mình muốn sử dụng thêm module mysql thì mình thêm trường dependencies và thư viện tương ứng vào file package.json

{
  "name": "demo1",
  "version": "1.0.0",
  "description": "demo create node.js project by npm",
  "main": "index.js",
  "scripts": {
    "test": "node test.js",
    "start": "node index.js"
  },
  "author": "stackjava.com",
  "license": "ISC",
  "dependencies": {
  "mysql": "^2.15.0"
  }
}

Sau đó chạy lệnh npm install nó sẽ tự động tạo file package-lock.json (dùng để mô tả) và folder node_modules để chứa module yêu cầu  cùng các module liên quan

Quản lý module với npm

Quản lý module với npm

Nhìn chung 1 project Node.js sẽ gồm 1 file package.json để mô tả project, folder node_modules dùng để chứa các modules sử dụng, 1 file main js (thường là file index.js) để chạy. Nếu bạn có nhiều file js hơn thì thêm nó vào các folder con giống như kiểu tổ chức package vậy.

Tạo project Node.js với npm (command line, cmd, terminal) stackjava.com

Okay, Done!

Vậy là mình đã thực hiện tạo project Node.js bằng lệnh npm, các bài sau mình sẽ hướng dẫn chi tiết hơn về các thành phần nâng cao của project Node.js

 

References: https://www.npmjs.com/

stackjava.com