转发自: https://yarnpkg.com/blog/2018/06/04/yarn-import-package-lock/?utm_source=tuicool&utm_medium=referral

Posted Jun 4, 2018 by Aram Drevekenin

For a while now, the JavaScript ecosystem is a host to a few different dependency lock file formats, including yarn’s yarn.lock and npm’s package-lock.json.

We are quite excited to announce that as of 1.7.0 yarn is able to import its dependency tree from npm’s package-lock.json natively, without external tools or clunky processes.

This will no doubt come as great news for developers working in mixed npm/yarn environments or wanting to try yarn out on existing projects.

All you need to do is issue the yarn import command in a repository with a package-lock.jsonfile, and yarn will use the resolution information from the existing package-lock.json file and a corresponding yarn.lock file will be created.

This feature is one of the first fruits of a continuing collaboration between the maintainers of the two package managers. We feel strongly about the two tools being aware of each other and providing an easy transition path between them. If you are interested or want to help, head over to the related GitHub issue.

How does it work under the hood

Previously, yarn import would rely on a package’s node_modules directory to determine the fixed versions to which the the new yarn.lock file needs to resolve its semver ranges. Now, it falls back to this behaviour if it cannot find a package-lock.json file.

When it does, yarn creates a dependency tree using npm-logical-tree from the package.json and package-lock.json in the project’s root directory. It then uses the fixed versions in that tree to create its own yarn.lock lockfile. The resulting yarn.lock will have all the exact fixed versions specified in package-lock.json. Ready to be installed and committed in your repository.

Limitations

The two lockfile formats and contents are different. Each have their own priorities, guarantees and trade-offs in terms of determinism, consistency and more. Since yarn.lock chooses only to store the logical dependency tree, preferring to future-proof for potential physical tree and hoisting optimizations, there are certain nuances that package-lock.json expresses that yarn.lockcannot.

One example would be:

// package-lock.json (slightly simplified for clarity)
{
"name": "nuanced-dependency-tree",
"dependencies": {
"a": {
"version": "9.9.9",
"requires": {
"c": "^1.0.0"
},
"dependencies": {
"c": {
"version": "1.0.1"
}
}
},
"b": {
"version": "8.8.8",
"requires": {
"c": "^1.0.0"
}
},
"c": {
"version": "1.0.5"
}
}
}

Here, we have both packages a and b which require the same semver range of package c^1.0.0 and get different versions: 1.0.1 and 1.0.5 respectively.

This would be imported to yarn as:

// yarn.lock (slightly simplified for clarity)
a@9.9.9
version "9.9.9"
dependencies:
c "^1.0.0" b@8.8.8
version "8.8.8"
dependencies:
c "^1.0.0" c@^1.0.0
version "1.0.5"

Here b’s dependency c would change its locked version from 1.0.1 to 1.0.5 because yarn.lock cannot express this duplication. Yarn chooses and aims to have a single resolved version for all compatible version ranges. While in most cases such minor changes should not have much effect - we encourage you to use this feature with care. You can still override ranges if you need to, using the selective version resolutions feature in yarn.

Future plans

Currently, we’re planning to add some warnings to users who use both yarn and npm in the same repository to install packages. If there’s a need, we might also try to expand this feature to other lock file formats. If you’d like to point out other issues of interoperability, or try your hand at fixing them - we encourage you to file an issue or better, fix one by sending a PR.

We highly recommend you to delete the package-lock.json file if you decide to use yarn in order to avoid future confusion and possible consistency issues.

 
 
 
 

Yarn import now uses package-lock.json的更多相关文章

  1. yarn import 使用package-lock.json

    yarn 1.7(目前最新的版本)支持npm 的package-lock.json 了 环境准备 安装更新yarn sudo npm install -g yarn 查看版本 yarn version ...

  2. Python 相对导入attempted relative import beyond top-level package

    ValueError: attempted relative import beyond top-level package 假设有如下层次包目录 project/ __init__.py mypac ...

  3. Python导入自定义类时显示错误:attempted relative import beyond top-level package

    显示这个错误可能有两个原因: 1.文件夹中没有包含__init__.py文件,该文件可以为空,但必须存在该文件. 2.把该文件当成主函数入口,该文件所在文件夹不能被解释器视作package,所以可能导 ...

  4. python项目内import其他内部package的模块的正确方法

    转载 :https://blog.csdn.net/u011089523/article/details/52931844 本文主要介绍如何在一个Python项目中,优雅的实现项目内各个package ...

  5. ValueError: attempted relative import beyond top-level package

    python 项目 在pycharm中, 在某个文件夹下: 右键--> mark directory as --> source root 如何在python脚本或者shell中 用代码实 ...

  6. npm和yarn的区别,我们该如何选择?

    首先,这两个都属于js包管理工具,都可以安装包或者模块yarn 是由facebook.google等联合开发推出的区别: npm 下载包的话 比如npm install它是按照包的排序,也就是队列挨个 ...

  7. npm和yarn使用

    npm和yarn使用 他们都属于js包管理工具,都可以安装包或者模块yarn 是由facebook.google等联合开发推出的 区别: npm 下载包的话 比如npm install,它是按照包的排 ...

  8. vue.config.json CopyWebpackPlugin 没有生效

    本地生效,服务器不生效. 因为是jenkinis构建,没有留意到报错.后来发现错误:ENOENT: no such file or directory, rename 解决方法就是:删除package ...

  9. 解决npm ERR!Unexpected end of JSON input while paring near (解析附近时JSON输入意外结束)'...."^2.0.0-rc.0","glob"'等npm install错误

    摘要 最近更新了一次node,但是更新后npm的命令总是会报 npm WARN deprecated fsevents@2.0.6: Please update: there are crash fi ...

随机推荐

  1. android系统提供的几种颜色Color

    http://blog.csdn.net/feiyangxiaomi/article/details/38338305 记录一下android自带颜色. Constants public static ...

  2. git仓库迁移的两种解决方案

    Git仓库迁移而不丢失log的方法 要求能保留原先的commit记录,应该如何迁移呢? 同时,本地已经clone了原仓库,要配置成新的仓库地址,该如何修改呢? 注意:如果使用了代码审核工具Gerrit ...

  3. 如何生成ssh密钥对

    答:执行以下命令即可,生成的密钥对在~/.ssh下,会生成两个文件,一个id_rsa和id_rsa.pub,前者是私钥,后者是公钥 ssh-keygen -t rsa -C "your_em ...

  4. WebApi Session支持

    代码: WebApiConfig using System; using System.Collections.Generic; using System.Linq; using System.Net ...

  5. POJ 3169 Layout(差分约束+最短路)题解

    题意:有一串数字1~n,按顺序排序,给两种要求,一是给定u,v保证pos[v] - pos[u] <= w:二是给定u,v保证pos[v] - pos[u] >= w.求pos[n] - ...

  6. IPv4数据报格式

    网络层的分组为数据报,数据报为首部和数据两部分组成,如下图所示. 首部的长度是以4个字节为单位,长度可以是20-60字节,这跟首部的HLEN字段有关. 版本: 这个4位字段定义了IP协议的版本,目前主 ...

  7. tomcat config https 443

    设置https: <Connector port="443" protocol="HTTP/1.1" SSLEnabled="true" ...

  8. R语言数据去重

    R语言常用的去重命令有unique duplicated unique主要是返回一个把重复元素或行给删除的向量.数据框或数组 > x <- c(3:5, 11:8, 8 + 0:5)> ...

  9. c++ 判断容器是否为空

    #include <iostream> #include <vector> #include <string> using namespace std; int m ...

  10. 80端口未被占用,无法启动wamp的解决方法(原创)

    起床之后想要弄弄侧边栏的东西,打开wamp居然无法启动apache服务,上网查了之后才知道是需要启动httpd.exe这个程序,测试了很久,80端口也没有被占用,点击启动apache服务的时候弹出co ...