---
title: "npm --save to Create package.json"
description: "Shell function to automatically create package.json when using npm install --save. Practical notes and code examples on npm."
url: "https://h3manth.com/scribe/npm-save-to-create-package-json/"
canonical_url: "https://h3manth.com/scribe/npm-save-to-create-package-json/"
markdown_url: "https://h3manth.com/scribe/blog/npm-save-to-create-package-json.md"
publishDate: "2014-03-09T00:00:00.000Z"
author: "Hemanth HM"
category: "node"
tags: ["node","npm"]
word_count: 138
reading_time_minutes: 1
x_markdown_tokens: 303
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# npm --save to Create package.json

> Shell function to automatically create package.json when using npm install --save. Practical notes and code examples on npm.

Until npm automatically creates package.json with `--save`, add this to your shell's rc file:

```bash
function npms() {
  [[ -f ./package.json ]] || echo {} > package.json && npm install --save $*
}
```

This adds to package.json in the current directory, creating one if not found.

## The Problem

Currently, `npm install --save` adds the dependency to the *nearest* package.json, searching up the directory tree. This can lead to dependencies accidentally being added to the wrong package.json.

npm also uses the real directory when handling symlinks, so if you're calling `npm install --save` from inside a symlinked dir, it'll find and add the dep to the package.json in the parent real directory.

The suggestion of calling `npm init` on missing package.json makes a lot of sense if install logic always just looked in the current working directory.
