npx là một cách rất hay để chạy mã Node và cung cấp nhiều tính năng hữu ích
Trong bài đăng này, tôi muốn giới thiệu một lệnh rất mạnh có sẵn trongnpmbắt đầu phiên bản 5.2, phát hành vào tháng 7 năm 2017:npx.
Nếu bạn không muốn cài đặt npm, bạn có thểcài đặt npx như một gói độc lập
npx
cho phép bạn chạy mã được xây dựng bằng Node và được xuất bản thông qua sổ đăng ký npm.
Dễ dàng chạy các lệnh cục bộ
Các nhà phát triển nút đã từng xuất bản hầu hết các lệnh thực thi dưới dạng các gói toàn cầu, để chúng nằm trong đường dẫn và có thể thực thi ngay lập tức.
Đây là một khó khăn vì bạn không thể thực sự cài đặt các phiên bản khác nhau của cùng một lệnh.
Đang chạynpx commandname
tự động tìm tham chiếu chính xác của lệnh bên trongnode_modules
thư mục của một dự án, mà không cần biết đường dẫn chính xác và không yêu cầu gói phải được cài đặt trên toàn cầu và trong đường dẫn của người dùng.
Thực thi lệnh không cần cài đặt
Có một tính năng tuyệt vời khác củanpm
, cho phép chạy các lệnh mà không cần cài đặt chúng trước.
Điều này khá hữu ích, chủ yếu là vì:
1) bạn không cần cài đặt bất cứ thứ gì 2) bạn có thể chạy các phiên bản khác nhau của cùng một lệnh, sử dụng cú pháp @version
Một minh chứng điển hình của việc sử dụngnpx
là thông quacowsay
chỉ huy.cowsay
sẽ in một con bò nói những gì bạn đã viết trong lệnh. Ví dụ:
cowsay "Hello"
sẽ in
_______
< Hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||Now, this if you have the cowsay
command globally installed from npm previously, otherwise you’ll get an error when you try to run the command.
npx
allows you to run that npm command without having it installed locally:
npx cowsay "Hello"
will do the job.
Now, this is a funny useless command.
Other scenarios include:
- running the
vue
CLI tool to create new applications and run them: npx vue create my-vue-app
- creating a new React app using
create-react-app
: npx create-react-app my-react-app
and many more.
Once downloaded, the downloaded code will be wiped.
Run some code using a different Node version
Use the @
to specify the version, and combine that with the node
npm package:
npx [email protected] -v #v6.14.3
npx [email protected] -v #v8.11.3
This helps to avoid tools like nvm
or the other Node version management tools.
Run arbitrary code snippets directly from a URL
npx
does not limit you to the packages published on the npm registry.
You can run code that sits in a GitHub gist, for example:
npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32
Of course, you need to be careful when running code that you do not control, as with great power comes great responsibility.
Download my free Node.js Handbook
More node tutorials:
- An introduction to the npm package manager
- Introduction to Node.js
- HTTP requests using Axios
- Where to host a Node.js app
- Interact with the Google Analytics API using Node.js
- The npx Node Package Runner
- The package.json guide
- Where does npm install the packages?
- How to update Node.js
- How to use or execute a package installed using npm
- The package-lock.json file
- Semantic Versioning using npm
- Should you commit the node_modules folder to Git?
- Update all the Node dependencies to their latest version
- Parsing JSON with Node.js
- Find the installed version of an npm package
- Node.js Streams
- Install an older version of an npm package
- Get the current folder in Node
- How to log an object in Node
- Expose functionality from a Node file using exports
- Differences between Node and the Browser
- Make an HTTP POST request using Node
- Get HTTP request body data using Node
- Node Buffers
- A brief history of Node.js
- How to install Node.js
- How much JavaScript do you need to know to use Node?
- How to use the Node.js REPL
- Node, accept arguments from the command line
- Output to the command line using Node
- Accept input from the command line in Node
- Uninstalling npm packages with `npm uninstall`
- npm global or local packages
- npm dependencies and devDependencies
- The Node.js Event Loop
- Understanding process.nextTick()
- Understanding setImmediate()
- The Node Event emitter
- Build an HTTP Server
- Making HTTP requests with Node
- The Node fs module
- HTTP requests in Node using Axios
- Reading files with Node
- Node File Paths
- Writing files with Node
- Node file stats
- Working with file descriptors in Node
- Working with folders in Node
- The Node path module
- The Node http module
- Using WebSockets with Node.js
- The basics of working with MySQL and Node
- Error handling in Node.js
- The Pug Guide
- How to read environment variables from Node.js
- How to exit from a Node.js program
- The Node os module
- The Node events module
- Node, the difference between development and production
- How to check if a file exists in Node.js
- How to create an empty file in Node.js
- How to remove a file with Node.js
- How to get the last updated date of a file using Node.js
- How to determine if a date is today in JavaScript
- How to write a JSON object to file in Node.js
- Why should you use Node.js in your next project?
- Run a web server from any folder
- How to use MongoDB with Node.js
- Use the Chrome DevTools to debug a Node.js app
- What is pnpm?
- The Node.js Runtime v8 options list
- How to fix the "Missing write access" error when using npm
- How to enable ES Modules in Node.js
- How to spawn a child process with Node.js
- How to get both parsed body and raw body in Express
- How to handle file uploads in Node.js
- What are peer dependencies in a Node module?
- How to write a CSV file with Node.js
- How to read a CSV file with Node.js
- The Node Core Modules
- Incrementing multiple folders numbers at once using Node.js
- How to print a canvas to a data URL
- How to create and save an image with Node.js and Canvas
- How to download an image using Node.js
- How to mass rename files in Node.js
- How to get the names of all the files in a folder in Node
- How to use promises and await with Node.js callback-based functions
- How to test an npm package locally
- How to check the current Node.js version at runtime
- How to use Sequelize to interact with PostgreSQL
- Serve an HTML page using Node.js
- How to solve the `util.pump is not a function` error in Node.js