npm scripts 参数传递:--
Published on February 15, 2025
Loading content...
https://docs.npmjs.com/cli/v6/commands/npm-run-script
CODEnpm run-script <command> [--silent] [-- <args>...] alias: npm run
在 npm 2.0.0 版本及以后,npm 提供了一种向脚本传递自定义参数的方式。这个功能的关键点如下:
--
)的特殊用途:
--
是一个特殊的分隔符--
后面的所有参数会被直接传递给指定的脚本CODEnpm run test -- --grep="pattern"
在这个例子中:
npm run test
是执行 npm 脚本的命令--
是分隔符--grep="pattern"
是传递给 test 脚本的参数Zshworkspace/npm-args-demo > npm test -- --grep="Addition" > npm-args-demo@1.0.0 pretest > echo 'Starting tests...' Starting tests... > npm-args-demo@1.0.0 test > mocha 'test/**/*.test.js' --grep=Addition Calculator Addition ✔ should add two positive numbers correctly ✔ should handle negative numbers 2 passing (3ms) > npm-args-demo@1.0.0 posttest > echo 'Tests completed!' Tests completed!
Zshworkspace/npm-args-demo > npm test --grep="Addition" > npm-args-demo@1.0.0 pretest > echo 'Starting tests...' Starting tests... > npm-args-demo@1.0.0 test > mocha 'test/**/*.test.js' Calculator Addition ✔ should add two positive numbers correctly ✔ should handle negative numbers Multiplication ✔ should multiply two positive numbers correctly ✔ should handle negative numbers 4 passing (3ms) > npm-args-demo@1.0.0 posttest > echo 'Tests completed!' Tests completed!
CODE"scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" },
CODE$ npm run build -- --profile > my-storefront@0.1.0 build > next build --profile