当前位置:网站首页>How to unify the use of package manager in a project?
How to unify the use of package manager in a project?
2022-06-30 01:00:00 【lyricist 】
Problems found in collaborative development , Because different package managers are used , As a result, all normal code in the local development environment is pushed to git Later, it failed unexpectedly in the construction on the computer of my colleagues , Why is that ? Let's check at the end node_module I found , Different package managers lead to the installation of different dependencies, which eventually leads to this problem .
Here we restrict the use of the same package manager through a pre installation script , Use at the same time yarn.lock or package-lock.json To lock our dependent versions .
Here are some solutions
One 、UserAgent programme
- adopt npm_config_user_agent To get the name and version of the currently executing package manager
- Restrict the installation of non allowed package managers by comparing names
Complete code
// preinstall.js
const allowPM = 'yarn'
const userAgent = process.env.npm_config_user_agent || ''
if (userAgent !== '') {
const pmName = userAgent.substring(0, userAgent.indexOf('/'))
if (pmName !== allowPM) {
console.warn(
`\u001b[33m This repository requires using ${
allowPM} as the package manager for scripts to work properly.\u001b[39m\n`
)
process.exit(1)
}
}
// package.json Add command
{
"scripts": {
"preinstall": "node ./preinstall.js"
}
}

Two 、ExecPath programme
- adopt npm_execpath To get the absolute path of the currently executing package manager
- Restrict non allowed package managers from performing installation by regularly matching names in the path
Complete code
const allowPM = 'yarn'
const execpath = process.env.npm_execpath || ''
if (!new RegExp(`${
allowPM}`).test(execpath)) {
console.warn(
`\u001b[33m This repository requires using ${
allowPM} as the package manager for scripts to work properly.\u001b[39m\n`
)
process.exit(1)
}
// package.json Add command
{
"scripts": {
"preinstall": "node ./preinstall.js"
}
}
3、 ... and 、only-allow programme
only-allow by pnpm The package manager organizes open source restriction schemes ,only-allow For internal use which-pm-runs To obtain the currently executed package manager, and then judge and intercept , Only after installation depends on adjustment. scripts The content of the , stay vite Used in the project .
// package.json This is the only step to add a command
{
"scripts": {
"preinstall": "npx only-allow yarn"
}
}

But in NPM v7 In later versions , The preinstallation script runs after the dependencies are installed , This undermines the expected behavior .
Can track official issue Treatment progress of :https://github.com/npm/cli/issues/2660
边栏推荐
- Comment personnaliser les modèles et générer rapidement le code complet dans l'idée?
- 浅析现代Web端im即时通讯开发技术
- VIM editor common instructions
- Outsourcing for 3 years is a waste
- How to switch to root in xshell
- [MySQL basic] general syntax 2
- Yunna | advantages of fixed assets system management, what are the characteristics of fixed assets management system
- Twitter launches the test of anti abuse tool "safe mode" and adds enabling prompt
- Text classification using huggingface
- 2020-12-03
猜你喜欢

Outsourcing work for three years, waste a step confused

岁月不饶人

Seata 与三大平台携手编程之夏,百万奖金等你来拿

清洁、对话、带娃,扫地机摆脱“人工智障”标签

SFDP super form development platform v6.0.4 was officially released

Too voluminous ~ eight part essay, the strongest king of interview!

Cloud, IPv6 and all-optical network

2020-12-03

【深度学习编译】算子编译 IR 转换

Seata et les trois plateformes travaillent main dans la main pour programmer Summer, un million de bonus vous attend
随机推荐
Quick Pow: 如何快速求幂
[mrctf2020]ezpop-1 | PHP serialization
Yunna | advantages of fixed assets system management, what are the characteristics of fixed assets management system
清洁、对话、带娃,扫地机摆脱“人工智障”标签
Too voluminous ~ eight part essay, the strongest king of interview!
Some thoughts on life
RubyMine开发工具,重构和意图操作
[Simulation Proteus] détection de port 8 bits 8 touches indépendantes
ResizeKit2.NET大小和分辨率独立
[concurrent programming] if you use channel to solve concurrency problems?
Twitter launches the test of anti abuse tool "safe mode" and adds enabling prompt
MySQL installation steps (detailed)
Taguchi experimental method
Text classification using huggingface
岁月不饶人
眼底出血术后需注意事项//每天必看
Birds in the corn field
我,33岁,字节跳动测试开发,揭开北京“测试岗”的真实收入
利用huggingface进行文本分类
A Yu's Rainbow Bridge