r/javascript • • May 26 '17

NPM v5.0.0 released, --save by default, lockfile, better support for git dependencies, better support for monorepos, and more!

http://blog.npmjs.org/post/161081169345/v500
420 Upvotes

90 comments sorted by

36

u/edanschwartz May 26 '17

Can someone explain the npm-shrinkwap.json vs package-lock.json thing? Why are there two? Do we need both?

21

u/enkideridu May 26 '17

Great question, was wondering that myself
Found this fresh documentation: https://github.com/npm/npm/blob/latest/doc/files/package-lock.json.md

One key detail about package-lock.json is that it cannot be published, and it will be ignored if found in any place other than the toplevel package. It shares a format with npm-shrinkwrap.json(5), which is essentially the same file, but allows publication. This is not recommended unless deploying a CLI tool or otherwise using the publication process for producing production packages.

If both package-lock.json and npm-shrinkwrap.json are present in the root of a package, package-lock.json will be completely ignored.

Seems to be 2 different names for the same file, shrinkwrap is publishable, package-lock is not

I wonder why they didn't repurpose shrinkwrap and add a allow-publication boolean field instead of creating a new file

26

u/maybekatz May 27 '17

So, the reason for the two files is wrapped up in a number of reasons: making npm more usable (and easier to understand), and retaining backwards compatibility.

As many folks have probably pointed out, we've had npm-shrinkwrap.json in npm since the dawn of time (definitely way before my time). At the same time, usability stuff around that particular file was.... suboptimal, so we couldn't just recommend people switch to it. People also had no idea it even existed -- partly because it was isolated to a non-default command, partly because who the fuck knows what "shrinkwrapping" means if you haven't actually looked into npm closely? Even know, people still think it's only now that npm has added a lockfile. lol.

Anyway, when we scoped out npm@5 earlier last year (uhh... January 2016? Something like that?) the stated goals were: rewrite the cache, and make npm-shrinkwrap.json what the community wants it to be (which included needing a discovery process).

Making shrinkwrap the default was part of that: people should just get it. It also turned out to be invaluable for performance boosts since it would effectively short-circuit a ton of tree building machinery in the installer. But npm-shrinkwrap.json was a footgun waiting to happen: npm publishes most files in your source directory by default, and people have been publishing shrinkwraps for years. We didn't want to break compatibility. With --save and shrinkwrap by default, there was a great risk of it accidentally making it in and propagating through the registry, and basically render our ability to update deps and dedupe... null.

So we chose a new name. And we chose a new name kind of all of a sudden. The new lockfile shares basically all of the same code, the exact same format (you can rename them between one another!). It's also something the community seems to understand: "it has a lockfile" seems to click so much faster with people. Finally, having a new file meant that we could have relatively low-risk backwards-compat with shrinkwrap without having to do weird things like allow-publication mentioned in the parent post.

Sorry if that's tl;dr, but I hope that helps clarify this. The idea is definitely for package-lock.json to be the Latest and Greatest in shrinkwrap technology, and npm-shrinkwrap.json to be reserved for those precious few folks out there who care very much about their libraries having an exact node_modules -- and for people who want CI using npm@>=2 to install a particular tree without having to bump its npm version.

11

u/GitHubPermalinkBot May 26 '17

I tried to turn your GitHub links into permanent links (press "y" to do this yourself):


Shoot me a PM if you think I'm doing something wrong. To delete this, click ^here.

1

u/curiousdannii May 27 '17

In what kind of projects is it actually helpful to commit the file? The whole point of semver and package.json is that updated compatible dependencies shouldn't need to be noted.

2

u/enkideridu May 27 '17

A patch or minor update could still break your build, because humans are fallible, and pretty frequently so. Not having your build break is way more important than allowing the tool to implicitly and automatically upgrade my dependencies

1

u/curiousdannii May 28 '17

So why not just use pinned versions in package.json?

4

u/enkideridu May 28 '17

Because that only pins your direct dependencies, but not the dependencies of your direct dependencies

We lost a morning once when our builds suddenly started breaking, even though our package.json had all the dependencies fixed. Turns out one of our sub dependencies depended on uglifyjs, which just released 2.6.3 that contained a bug

Shrinkwrapping ever since before switching to yarn's lockfiles

2

u/curiousdannii May 28 '17

Ah yes, that does make perfect sense, thanks for the explanation!

1

u/aeflash May 28 '17

The package lock also allows you to effectively pin versions of transitive dependencies as well, even if you pin in your package json.

2

u/ForAllThat May 26 '17

Would also like to know..

69

u/greymalik May 26 '17

Can I stop using yarn now, or does it still have some advantages? And are there any efforts underway for an npm/yarn merge ala node/io.js, or are we going to keep jumping back and forth as each one-ups the other in a feature war?

33

u/[deleted] May 26 '17

I'm wondering this myself. We switched to Yarn because it was insanely faster and the lock file feature. Npm5 now appears to be faster than Yarn and has a lock file... They should just kill Yarn.

57

u/vexii May 26 '17

i think we are seeing these features because yarn have show it's good/what people want (at least on an opt in basis). let yarn be the "playground" for experimental features/ideas, like coffee/typescript influenced es6

3

u/maybekatz May 27 '17

it's a bit of both, tbh: it's almost all stuff we planned, but having someone else show that it was actually a good decision and people were happy about it made it much easier to go ahead, instead of taking more careful steps. There's only so much cheese-moving we can risk doing without breaking a great number of people who rely on exactly the way npm does things. :) I, for one, think it's great to have an unstable playground that can afford to ruin people's day (and we've been talking about having a "canary" npm where we can try new ideas on responsive users)

14

u/dmitri14_gmail_com May 26 '17

Is it anywhere documented to be faster than yarn?

18

u/OverBelief May 26 '17 edited May 30 '17

I can't find any "official documentation", but over on HackerNews there have been some users who have ran their own code with Yarn && versions of NPM.

This user had these results

Also ran some numbers here (with warm cache. node 6.10.3).

  • yarn: 25s .

  • npm@5: 28s .

  • npm@4: 63s .

  • npm@3: 68s .

Another user had these results

On cold cache: Yarn: 20.94 seconds NPM5: 21.11 seconds
With cache: Yarn: 10.35 seconds NPM5: 15.20 seconds

So, in general, it seems the speeds are very close but that Yarn is still a tiny bit faster.

0

u/[deleted] May 26 '17

[deleted]

16

u/dankelleher May 26 '17

With those small differences between yarn and npm5 we don't, really (although those split seconds will add up in e.g. a CI environment). But the difference between npm4 and yarn was huge.

3

u/honigbadger May 27 '17

Because, when using something "a little" less powerful that your average MacBook Pro (like a docker container) the algorithm used to resolve dependencies for packages makes big difference in build times. Sometimes you're using containers on a dev env, sometimes on staging and sometimes even on prod, that's just one case where speed matters... But in general we should not be thinking about "few seconds more or less on powerful hardware" instead we should be aiming to build on top of solutions that work fast in almost any hardware used out there. Not everyone is developing on i7's with 16GB of RAM and the lower you go on the specs scale, the more precious the seconds you're getting off one tool or another

3

u/[deleted] May 27 '17

While that is true, the difference in build times was still not that huge. NPM install is not the longest thing that happens when i build or test. And i'd rather focus on my live reloading stuff instead. I can understand the need for it being faster, but i've never really thought "oh boy that NPM installation sure takes up a lot of my day".

3

u/learn_to_model May 27 '17

As someone who unfortunately has to fiddle with different CI configs and legacy deployment setups at work, I kindly disagree. We're currently moving everything over to docker and during our first experiments we obviously needed to get familiar with it and rebuild our containers constantly. yarn was a huge timesaver for that. With npm it would've taken ages to test

2

u/kenman May 27 '17

I work with a couple of megarepos (really complex projects), and when I still had my macbook air, a fresh install would take anywhere from 30-45 mins, with at least 90% of that time being consumed by npm i. With Yarn, that time went down considerably, to around 15 mins, and when using Yarn's cache, that went down to under 5 mins.

Due to having to support multiple versions of those projects, it wasn't uncommon to git clean -xfd && git checkout release/some-other-version && npm i several times a day. Yarn literally gave me several hours a week back that I otherwise would have wasted waiting on NPM. That's huge.

1

u/kylemh Jun 09 '17

Depending on how large the project is the speedier dependency download saved minutes sometimes. Before npm v5, it was also better because of lockfiles.

The real question is, what has npm v5 done to encourage a switch back from yarn? Now, they offer the same thing except yarn is just a tiny bit faster.

2

u/subvertallchris May 26 '17 edited May 26 '17

And even if it is, is yarn so slow and the improvement so profound that it's worth migrating? I doubt it.

4

u/Akkuma May 26 '17

Yes the improvements to npm 5 are profound. You can now use semver for git dependencies, while yarn still seems to be buggier on projects I've used it on.

1

u/subvertallchris May 27 '17

That is a nice improvement. I stand corrected, I guess?

But really, I don't understand all the yarn hate. It's worked perfectly for me since I switched to it and with every project since then. It feels like a slicker Bundler (Ruby).

3

u/[deleted] May 26 '17

I used yarn on a recent project that I forked from GitHub that wouldn't work until I just went ahead and did an 'npm install'. Anyone else had this problem?

0

u/[deleted] May 26 '17

[deleted]

2

u/BritainRitten May 27 '17

The command is just 'yarn' to install all dependencies listed in package.json

0

u/[deleted] May 27 '17

[deleted]

1

u/[deleted] May 27 '17

I know it should be equivalent but it didn't work for me and npm install did.

1

u/chesterjosiah Staff Software Engineer / 18 yoe May 27 '17

Me too boss, I'm agreeing with you.

→ More replies (0)

1

u/[deleted] May 27 '17

Regardless of the downvotes you're getting, i am willing to wait an extra 20 seconds on initial build if it works. How often are you doing that? With npm V5 improving load time, why not?

1

u/patrickfatrick May 29 '17

The one thing Yarn still has that is killer is yarn upgrade-interactive.

0

u/Akkuma May 26 '17

https://github.com/JamieMason/npm-cache-benchmark

Yarn really has very little reason to exist now. The only one that has a reason to exist as well is pnpm due to it having additional goals of reducing memory consumption on disk.

5

u/vinnl May 26 '17

Doesn't really look like it, although there's no word about parallel installations which I seem to recall was the main reason Yarn was fast? It's a shame that there's no mention of Yarn here. Let's wait and see what the Yarn maintainers will have to say about this.

12

u/blood_bender May 26 '17

Uh, excuse me, but npm doesn't have npm create.

Dealbreaker.

10

u/redonkulus May 26 '17

Sarcasm?

17

u/blood_bender May 26 '17

Lol very poorly received sarcasm, apparently.

I can't believe they put resources into yarn create. So ridiculous.

4

u/redonkulus May 26 '17

Ya I agree. Seems like a weird feature to add.

7

u/[deleted] May 27 '17

Yarn is from Facebook. React is from Facebook. Create React App is from Facebook. Facebook devs probably create 10 new React apps per day to test some prototype or feature.

Are you still surprised?

2

u/jocull May 27 '17

Like the IO.js split then merge. Bring them together again! â¤ī¸

-1

u/markzzy May 26 '17

The churn is real...

29

u/the-prophet-harambe May 26 '17

to install: npm install npm@5 -g

6

u/[deleted] May 26 '17

Thank you! I tried npm@latest and got nowhere.

8

u/TheDarkIn1978 May 26 '17

Yup, as revealed in npm view npm, their latest is currently still set to 4.6.1 and next is currently set to 5.0.0

 { latest: '4.6.1',
     next: '5.0.0',
     'latest-2': '2.15.12',
     'next-2': '2.15.12',
     'latest-1': '1.4.29',
     lts: '2.15.11',
     'latest-3': '3.10.10',
     'next-3': '3.10.10',
     'latest-4': '4.6.1',
     'next-4': '4.6.1' },

Alternatively, you can always check what's available with npm view npm version -json. The JSON flag forces the complete print out of every version instead of truncating the list with ellipsis. So it will show this:

...
  "4.5.0",
  "4.6.0",
  "4.6.1",
  "5.0.0"
]

instead of this:

...
  '2.1.10',
  '2.1.11',
  '2.1.12',
  ... 160 more items ]

24

u/TheDarkIn1978 May 26 '17

npm will now scold you if you capitalize its name. seriously it will fight you

defiant fist in the air

You'll never stop me from calling it "NPM" on my resume, NNNNNNNNNNEEEEEEEEEEEEVVVVVVVVVVVVEEEEEEEEEEEEEEEEERRRRRRRRRRRRRRRRRRRRR!!!

11

u/maybekatz May 27 '17

fite me noob 👊đŸŧ

fite me now

9

u/jcready __proto__ May 26 '17

Newer?

1

u/cowjenga May 27 '17

Who types CLI commands in caps anyway? I don't think I've ever seen that

1

u/[deleted] May 30 '17

"NPM" on my resume

Why even put it there?

1

u/TheDarkIn1978 May 30 '17 edited May 30 '17

I have a "Skills" section on my CV that contains 2 lists under the subheadings "Developer Tools / IDEs" and "Languages / Libraries". There's ~40 items listed, but it takes up less than 25% of my one page resume. I think it's important to list an overview of my professional skillset on my resume so potential employers have an idea of what to expect before reviewing my accompanying online portfolio.

1

u/hackel May 26 '17

How would that even work? Renaming the binary or symlinking? That's bizarre.

Edit: I tried symlinking it to NPM and it worked just fine. No idea what they're talking about, unless it was just a joke.

3

u/arstechnophile May 26 '17

I think it was a joke. Invoking it as "NPM" on Windows (which is case-insensitive) results in normal behavior.

3

u/NoInkling May 27 '17

Is Node 8 gonna come with this?

3

u/frontsideair May 27 '17

They implemented some of the best features of Yarn, which is pretty nice.

3

u/Arsketeer_ May 29 '17

Yarn is the future. I'm sticking with it. npm has shown time and again that they are incapable of making good technical decisions unless the decisions are made for them.

It's truly dumbfounding how the makers of a tool that's the foundation of an entire ecosystem can't understand the importance of basic lockfile features or remotely fast install times, to the point where someone has to go out and write a competing product to make a point. Let's not forget how long it took to get rid of the infinite dependency trees in npm2 as well. Oh, and how npmjs.org somehow allowed users to withhold packages, which led to the left-pad incident. How on earth does that even happen? Allowing a package to get pulled from the repo when thousands of other packages depend on it, including giants like Babel?

And it's not like yarn is that hard to switch to. JS fatigue doesn't even apply; they're both package managers and accomplish the same thing with very similar commands and processes, same as all other package managers. Yarn is simply objectively better in every way, aside from npm coming built-in.

3

u/papers_ May 26 '17 edited May 26 '17

Yarn is still beating npm on my project by 10s. Looks like I'll be sitcking with it for a while longer. Will definitely use v5 on CI though.

1

u/tobegiannis Jun 02 '17

Probably not a good idea, stick with one or the other and get deterministic installs/builds.

1

u/markzzy May 26 '17 edited May 26 '17

I'm sorry but I find it extremely hilarious that this version adds most if not all the features yarn touts. I never understood why the yarn team didnt just submit there features directly to the npm codebase. Oh well.

26

u/Geldan May 26 '17

They talk about that here: https://code.facebook.com/posts/1840075619545360.

tl;dr, they tried to get npm to behave, but it was a nightmare.

Also, they probably just didn't want to deal with npm who are known for letting politics dictate engineering direction more often then they should.

4

u/markzzy May 27 '17

Not sure why you're getting downvoted. You've provided a helpful link. But thanks for that. Upvote for you!

3

u/vinnl May 27 '17

tl;dr, they tried to get npm to behave, but it was a nightmare.

But the way they did that was by using existing npm features, not modifying npm's code and submitting pull requests. It is a somewhat valid question to ask why they didn't attempt that, I think.

1

u/Geldan May 27 '17

The changes they would have had to make couldn't be classified as "modifying." It would have basically been a complete rewrite. If you have to do all that work anyway why would you choose to deal with the pull request process and thus submitting yourself to the will of other people who clearly didn't share your vision?

1

u/vinnl May 27 '17

So is npm@5 basically a complete rewrite as well? And how did they know they "clearly" didn't share the vision even though npm has now implemented the most important features?

Note that I'm not saying there are no good reasons, just that it's striking that there's no mention of them trying it in the announcement post, which leads me to wonder whether it was even considered.

5

u/maybekatz May 27 '17

npm5 is a rewrite of a fairly core component of npm: the entire package downloading and caching infrastructure. Everything else stayed mostly the same -- the other big chunk that was changed was the way the installer interacts with package-lock.json (nÊe npm-shrinkwrap.json), in order to take advantage of it having sha512sums, and short-circuiting a bunch of early-phase work we'd otherwise need to do.

The rest of the CLI code is mostly the same -- we did some refactoring to use promises in more places, some fixes to make the new changes uhhh... work at all, but nothing earth-shattering.

The entire download and caching infrastructure now exists in standalone packages now: https://npm.im/pacote (literally can do pacote.extract('somepkg@^3', './here')), https://npm.im/cacache (the new cache!), and https://npm.im/make-fetch-happen (the new http client!). Plus a few others. node-tar was upgraded as part of all this but hasn't landed yet.

We're working on tearing apart the CLI into smaller chunks that people can use to build their own CLIs without having to first reinvent the universe in their own image. The dream is for people to be able to spin up custom, full-featured package managers in an afternoon that can compete with the latest npm right out the door, and for npm's overall codebase to be in small enough chunks that people can make significant contributions without having to know the entirety of the (massive, terrifying) npm codebase. Seriously, it took me like a year to catch my footing. And that's pretty much everyone else's experience on the team.

2

u/tbranyen May 27 '17

That's a pretty great vision and aligns with reasons I never contributed to npm. Too massive and unconventional. I'm currently experimenting on an HTTP2 package manager geared towards ESM projects and these tools will help immensely to integrate with npm.

Thanks!

2

u/maybekatz May 27 '17

We're redesigning the registry API, and http2 is one thing we're looking at closely. ESM-awareness as well :)

Just sayin' that if you integrate your http2 stuff with make-fetch-happen, I'd totally consider the patch, and npm would be able to take advantage of it as soon as a service is available for it 👌

1

u/vinnl May 27 '17

Thanks for the details, that's really interesting! So has the Yarn team contacted you to try and see whether npm could be modified to incorporate the features Yarn introduced (and that have now been incorporated into npm)? And do you happen to know why that route was not chosen - was it to make sure it was a desirable direction for npm?

6

u/maybekatz May 27 '17

The Yarn team has never expressed the intention of moving their efforts to anything but Yarn, regardless of the state of npm, nor have they expressed the remotest intention of contributing to our codebase in any significant way. They even seem to have avoided most of the dependencies npm uses (many of which take care of doing certain tasks in npm-compatible ways). I don't think I have ever seen a patch come through our issue tracker by any member of the Yarn team, except for one person who started bouncing around all our deps and adding "files: [...]" to them so we'd stop publishing test/ for the ones that still did. And that was a long time ago now.

To be fair, the way the project was running as of last year, we were being much much more conservative about making big changes to the codebase or UX, even with the changes that were scheduled for Q1 2017. We've changed that now, so change is happening a lot faster (while still being careful about backwards-compat), and we're putting more effort into making the codebase easier to pick up and contribute to AND letting community devs have more influence over its direction. A lot of the little details in npm5 were things people directly asked us to include, some of which were turned down years ago (before my time!), and we just went "ok sure why not that sounds easy enough". :)

(For the record: the cache rewrite, which enabled offline, did most of the speed improvements, and enabled checksums in the lockfile, as well as "make shrinkwrap what the community wants it to be", has been on our roadmap for about 18 months now, and has been on our backlog as an important item for something like 4 years. I have a conference talk video where I'm outlining the plan, before Yarn was even announced.)

2

u/vinnl May 28 '17

Thanks, this clears up a lot. It's a shame you the Yarn developers didn't jump in, although it does sound indeed like it probably wouldn't have been that easy at first either. Then again, now they're stuck with maintaining a package manager that might lose a lot of the interest it initially gathered, so...

2

u/Geldan May 27 '17 edited May 27 '17

I guess it seemed clear to me because of the particular struggles I went through. When I switched asset bundling over to gulp/webpack in October of 2015 I knew I needed reproducible builds, so naturally I used shrinkwrap.

Shrinkwrap was a nightmare and I dreaded modifying dependencies when I used npm. Even worse was when other devs who hadn't devoted as much time as I had to understanding how it works needed to add dependencies. Forgetting to shrinkwrap or doing it incorrectly had unfun consequences that took time to work through. To me it was a huge issue that nobody at npm seemed to care about (or at least not the right people).

Fast forward a year to October 2016 when yarn came out and my builds got much faster and all of my shrinkwrap issues magically went away. I now have the ability to effortlessly make quick, reproducible builds.

npm had a lot of time to make this happen before yarn, but they didn't. That's why I believe that the vision wasn't shared.

0

u/vinnl May 28 '17

An npm developer just gave some clarification: it had been on their radar, but they were more conservative back then (although they also hadn't been contacted by Yarn).

2

u/Geldan May 28 '17

Yeah, but "radar" is a very loose term. Anywhere I have ever worked this just means that the idea is recognized as good, not necessarily that it will be done anytime soon.

I definitely believe that it was "on the radar," because I know there are some great engineers at npm who saw the value and would likely have advocated for it. I just don't believe that the people making choices about how time is allocated shared that vision.

With past large changes to npm like when it went to flat dependency structure, the public knew it was coming for a long time. As far as i know there was no such public knowledge about these changes until after yarn came out. This certainly makes it look like a reaction.

1

u/vinnl May 29 '17

That is an accurate analysis, I think.

2

u/temp069859085 May 26 '17

This version does not add most if not all the features yarn touts. It adds the superficial stuff, and what can be easily integrated. Yarn is fundamentally different, and still fundamentally more reliable and faster.

6

u/markzzy May 27 '17

Not sure what you mean by "superficial stuff". If you read the release notes you clearly see that npm offers a lot of yarn's features in its v5 version. You work for yarn or sumthin? Couldnt help but notice you created a new profile just to respond to my comment.

-1

u/the-prophet-harambe May 26 '17

lul these patch notes

0

u/OogieFrenchieBoogie May 27 '17

Since no one mentionned it :

--save by default, at least !

-16

u/hackel May 26 '17

Why? Does anyone even use npm anymore? I thought we had all just abandoned it and moved over to Yarn.

3

u/[deleted] May 27 '17

The Yarn CLI really messes with me. It just doesn't feel as intuitive as npm, it's been over a month now and I still have to constantly look up commands for Yarn.

0

u/[deleted] May 26 '17

[deleted]

0

u/[deleted] May 26 '17

[deleted]

4

u/myschoo May 26 '17

It's been only published under @next while npm by default installs @latest.

12

u/[deleted] May 26 '17

[removed] — view removed comment

7

u/greymalik May 26 '17

Anyone have a TLDR? Er, TLDL?

-3

u/[deleted] May 27 '17

RemindMe! 2 days "this"

4

u/digitallimit May 26 '17

Is offline install (from cache) supported by npm?

3

u/blood_bender May 26 '17

It is as of V5, yeah.