This can be now achieved by using the Cache
task. More info can be found on MSDN article (it also has many examples for well-known apps) but in general it goes like this:

or in YAML
steps:
- task: Cache@2
displayName: 'Cache npm'
inputs:
key: 'npm | "$(Agent.OS)" | package.json'
path: '$(npm_config_cache)'
The idea is that you specify a cache key, which could be a string or a file content (exactly what you're asking for) and the path to cache. You don't need a separate restore task, this one works in both directions.
Note, that for this example you should also define the npm_config_cache
environment variable.