npx & gist & npm package All In One
npx & gist & npm package All In One
npx & gist & npm package
# execute gist code `bin`
$ npx https://gist.github.com/xgqfrms/1981c08fcb7e09056e1c5d9192f272c8
npm
$ npm i -g rollup
$ npm i -S rollup
$ npm i -D rollup
https://www.npmjs.com/package/npm
npx
- npx 先从本地已安装的 .node_modules 中加载对应的模块,找到了模块就直接使用,不需要再次安装模块;(npm ci)
- 如果本地找不到模块,npx 会从远端 npm 下载依赖的模块到一个临时的目录,使用后会立即销毁该目录;
- 可以手动指定仅使用本地的模块, 还是每次都从远程重新安装模块
$ npx rollup
# --no-install,让 npx 强制使用本地模块,不下载远程模块,可以使用--no-install参数;如果本地不存在该模块,就会报错;
$ npx --no-install rollup
# --ignore-existing, 忽略本地的同名模块,强制安装使用远程模块
$ npx --ignore-existing rollup
https://www.npmjs.com/package/npx
https://github.com/npm/npx
math.js
#!/usr/bin/env node
// math.js & `bin` package.json
console.log('math.js 👻');
const add = (a, b) => {
return a + b;
}
const math = {
add,
};
// test case
const result = add(1, 2);
console.log('result =', result, result === (1 + 2) ? '✅' : '❌');
// ESM
// export default math;
// Declaration or statement expected.ts(1128) ❌
// export math;
// CJS
// module.exports.add = add;
module.exports = math;
$ node ./math.js
# result = 3 ✅
- npx error ❌
# gist is not an npm module, not exists the package.json file ❌
$ npx https://gist.github.com/xgqfrms/4a17f74c9c29866e2d0149e2f0feeb70
- npx ok ✅
# gist is an npm module, and exists the package.json file ✅
$ npx https://gist.github.com/xgqfrms/1981c08fcb7e09056e1c5d9192f272c8
$ npx gist:1981c08fcb7e09056e1c5d9192f272c8
github gist & npm module
demos
PM2
# global
$ pm2 start server/index.js
# local
$ ./node_modules/pm2/bin/pm2 start server/index.js
# npx 等价于 ./node_modules/pm2/bin/ ✅
$ npx pm2 start server/index.js
nodemon
# global
$ nodemon ./server.js
# local
$ ./node_modules/nodemon/bin/nodemon.js ./server.js
# npx 等价于 ./node_modules/nodemon/bin/ ✅
$ npx nodemon server.js
refs
PM2 & nodemon & Node.js Deamon All In One
https://www.cnblogs.com/xgqfrms/p/13621076.html
https://www.npmjs.com/~xgqfrms
https://www.ruanyifeng.com/blog/2019/02/npx.html
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16246009.html
未经授权禁止转载,违者必究!