有没有办法指定新版本的npm只使用"lockfileVersion": 1?还是说我们必须让所有的开发者使用同一个版本的npm?
我会建议你固定Node/NPM版本,并在你的环境(开发、登台和生产)中对齐它。
你可以通过在你的项目中添加.nvmrc文件来利用nvm来管理Node版本(不要忘记将它存储在你的源代码控制中)。
例如,.nvmrc看起来像:
$ cat .nvmrc
14.15.0
然后,你可以使用nvm install && nvm use来使用Node的固定版本。
NPM也支持引擎:
You can specify the version of node that your stuff works on:
{ "engines" : { "node" : ">=0.10.3 <0.12" } }
And, like with dependencies, if you don't specify the version (or if you specify "*" as the version), then any version of Node will do.
If you specify an "engines" field, then npm will require that "node" be somewhere on that list. If "engines" is omitted, then npm will just assume that it works on Node.
You can also use the "engines" field to specify which versions of npm are capable of properly installing your program. For example:
{ "engines" : { "npm" : "~1.0.20" } }
Unless the user has set the engine-strict config flag, this field is advisory only and will only produce warnings when your package is installed as a dependency.
当使用引擎字段并在版本约束不满足时使npm失败时,在.npmrc文件中设置engine-strict=true(因为默认为false)或作为npm_config_engine_strict=true环境变量
如果设置为true,那么npm将顽固地拒绝安装(甚至考虑安装)任何声称与当前Node.js版本不兼容的包。
这可以通过设置——force标志来覆盖。
另一种方法是使用Docker容器作为开发和执行的运行时环境,这意味着你既不需要安装Node,也不需要安装NPM。如。
$ mkdir my-project
$ cd my-project
$ docker run --rm -it -v $PWD:/app --entrypoint /bin/bash --workdir /app node:14.15.0
root@4da6ee3c2ac0:/app# npm init -y
Wrote to /app/package.json:
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
root@4da6ee3c2ac0:/app# npm install
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN app@1.0.0 No description
npm WARN app@1.0.0 No repository field.
up to date in 1.694s
found 0 vulnerabilities
root@4da6ee3c2ac0:/app# exit
exit
$ ls -x1
package-lock.json
package.json
如你所见,既没有Node,也没有NPM:
为新项目创建新目录
旋转一个Node Docker容器,它随Node和NPM一起提供
创建一个新项目(npm init -y)
退出Docker容器
列出容器旋转的工作目录中的文件
由于上面的docker run命令很长,您可能希望利用docker-compose来实现更精简的工作流。