如果两个不同的开发人员在最初使用包锁创建的项目中使用不同版本的node(12/15)和npm(6/7)。json "lockfileVersion": 1,当开发者使用npm 7x安装新包时,似乎包锁。使用"lockfileVersion": 2重新创建json。

这似乎给使用npm v6的开发人员带来了问题,因为它试图与lockfileVersion 2一起工作,但它最终产生了新的差异。

npm WARN read-shrinkwrap这个版本的npm与lockfileVersion@1兼容,但是package-lock。json为lockfileVersion@2生成。我会尽力做到最好的!

有没有办法指定新版本的npm只使用"lockfileVersion": 1?还是说我们必须让所有的开发者使用同一个版本的npm?


当前回答

有没有办法指定新版本的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来实现更精简的工作流。

其他回答

据我所知,npm文档说npm v6将与版本2的锁文件一起工作,尽管有警告,所以你不需要做任何在接受的答案中建议的事情,可以安全地忽略警告消息。

在npm 7发布说明中,他们说:

需要注意的一个变化是新的lockfile格式,即 向后兼容NPM 6用户。锁文件v2解锁 能够进行确定的和可复制的构建以生成 包树。

在npm文档中,它说(我的重点):

lockfileVersion An integer version, starting at 1 with the version number of this document whose semantics were used when generating this package-lock.json. Note that the file format changed significantly in npm v7 to track information that would have otherwise required looking in node_modules or the npm registry. Lockfiles generated by npm v7 will contain lockfileVersion: 2. No version provided: an "ancient" shrinkwrap file from a version of npm prior to npm v5. 1: The lockfile version used by npm v5 and v6. 2: The lockfile version used by npm v7, which is backwards compatible to v1 lockfiles. 3: The lockfile version used by npm v7, without backwards compatibility affordances. This is used for the hidden lockfile at node_modules/.package-lock.json, and will likely be used in a future version of npm, once support for npm v6 is no longer relevant.

这就是为什么他们可以自动将锁文件从v1升级到v2,而不会破坏任何东西。

npm WARN read-shrinkwrap这个版本的npm与lockfileVersion@1兼容,但是package-lock。json为lockfileVersion@2生成。我会尽力做到最好的!

为了解决此问题,可以使用该命令

npm i -g npm@latest

全局和运行命令

npm i npm@latest

在项目文件中帮我解决了这个问题。

尝试移除包锁。然后再次运行NPM install。

从8.1.0版本开始,在npm中有一个标志——lockfile-version,你可以用它来覆盖默认的锁文件版本:

npm i --lockfile-version 3

这是原始PR的链接。

有没有办法指定新版本的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来实现更精简的工作流。