If you use NPM in your daily workflow, I believe the tips and tricks introduced in this article will be helpful to you
[Recommended course:node.js course]
[Recommended article:What is npm、How to install and configure npm】
Generate package.json
We usually execute npm init and then start adding the information requested by npm. However, if we don't care about all this information and want to keep the default values, then we press enter for every piece of data npm requests. To avoid this, you can type npm init -y. This way you can skip asking questions.
Note: You can also use npm init --yes, which has the same effect.
Installing modules
You can use the simpler npm i instead of npm install.
Install multiple modules at once
You do not need to type an npm install command for each module, for example:
npm i gulp-pugnpm i gulp-debugnpm i gulp-sass
You can install all these modules at once by running just one command:
npm i gulp-pug gulp-debug gulp-sass
There is also an easier way, if all names start with the same prefix , you don’t need to type the entire name:
npm i gulp{-debug,-sass,-pug}
Use install flags (installation parameters) shortcut
If you want to install software package and save it as a production dependency, you would normally do this
npm i gulp --save-prod
You can use the -P shortcut, like this:
npm i gulp -P
The same goes for development dependencies, instead of typing the full --save-dev flag, you can use the -D shortcut, like this:
npm i gulp -D
By default, when you run npm install without any flags (arguments), npm will add the package as a dependency to the package.json file. If you want to prevent this, install with --no-save flags (parameter) as follows:
npm i gulp --no-save
Get package information
The following command will display relevant information about the vue package:
npm view vue or npm v vue
If you just want to get the latest version of the package, you can try the following command:
> npm v vue version> 2.5.17
If you want to get the complete version list of npm packages, please try the plural form
> npm v vue versions> [ '0.0.0', '0.6.0', '0.7.0', ... '2.5.15', '2.5.16', '2.5.17-beta.0', '2.5.17' ]
Install a specific version of a package
If you want to install a version instead of the latest version of a package, you can type:
npm i vue@2.5.15
Since it's easier to remember names than numbers (at least for me), you can use a dist-tag list of names and run the npm v command to get the list, like this:
npm i vue@beta
Search for package
Sometimes you may not remember a package that you or your friends recommended some time ago ( the exact name of the package). In this case, you can use npm search and perform the search directly in the terminal:
npm search gulp debug
or
npm s gulp debug
This will print a list of packages with description, author and some other information:
Uninstall package(package)
if You don't want to open the package.json file and manually remove the dependencies from there, you can remove it using:
npm uninstall vue
This will remove the dependencies from the node_modules folder and package.json Delete the package from the file. Of course, you can use rm, un or r to achieve the same effect, for example:
npm rm vue
If for some reason you just want to delete the package files from the node_modules folder , but still save it as a dependency in the package.json file, you can use the --no-save parameter:
npm rm vue --no-save
List dependencies
If you want to see the list of project dependencies, you can use
npm ls
This will list all the dependencies in the package.json file and their All dependencies. If you just want to list your dependencies you can do this
npm ls --depth=0
这将打印出这样的东西:
├── jquery@3.3.1├── vue@2.5.17└── yarn@1.12.3
当然,如果要查看所有全局安装的包的列表,可以使用 -g 标志
npm ls -g -depth 0
运行测试
你可以使用 npm run tests 运行测试,但你可以用 npm test 甚至更简短的 npm t 代替。
显示可用的 script
有时,我们希望查看 package.json 文件中包含的脚本。 我们当然可以打开 package.json 文件,但我们也可以这样做:
npm run
如果在 package.json 文件中有这样的配置,如下所示:
"scripts": { "test": "jest", "build": "gulp build"}
那么 npm run 命令将显示以下内容:
Lifecycle scripts included in npm: test jestavailable via `npm run-script`: build gulp-build
从 Github 仓库安装 package(包)
你可以直接从 Github 仓库安装一个包:
npm i https://github.com/sindresorhus/gulp-debug
或者你可以省略域名部分
npm i sindresorhus/gulp-debug
打开包的 Github 页面
你当然可以通过 Google 搜索,然后查找该页面,或者你可以执行以下操作:
npm repo create-react-app
无需安装软件包即可执行上述命令。
列出所有可用的 NPM 环境变量
你可以通过运行以下命令来查看可供我们使用的 NPM 变量的完整列表:
npm run env | grep npm_
上面的命令将打印如下内容:
npm_config_fetch_retry_maxtimeout=60000npm_config_tag_version_prefix=vnpm_config_strict_ssl=truenpm_config_sso_type=oauth...
这些变量的好处是它们可以在你的脚本中使用,你甚至可以创建自己的 NPM 环境变量,让我们看看如何创建。
添加自己的 NPM 变量
你可以通过向 package.json 文件添加自己的 NPM 变量。 它可以是任何 key,但我更喜欢将所有 NPM 变量放在 config key 中,以保持结构有序。 像这样:
"config": { "build_folder":"./dist" }
现在,如果你使用前面讨论的命令 npm run env | grep npm_ 列出你的变量,你会看到你的新变量在那里:
npm_package_config_build_folder=./distnpm_config_fetch_retry_maxtimeout=60000npm_config_tag_version_prefix=vnpm_config_strict_ssl=truenpm_config_sso_type=oauth...
默认情况下,npm 会将你的变量命名以 npm_package 为前缀,并保持其在 package.json文件中的结构,即 config_build_folder 。
在 NPM script 中使用 NPM 变量
一旦你看到了完整的变量列表,并且你希望在 script 中使用这些变量中的任何一个的值,那么你就可以在 package.json 中执行此操作(请参阅上一节中变量 npm_package_config_build_folder 的值)
"scripts": { "build": "gulp build --dist $npm_package_config_build_folder"}
一旦你用 npm run build 运行这个命令,它将被执行为
bash 代码:
gulp build --dist ./dist
总结:以上就是有关NPM 的一些有用的提示和技巧,希望对大家有所帮助。