Is package.json supposed to be manually edited? Couldn't a program like npm just look through the files, see the "require" statements, and then use that to put the necessary entries in the package.json file? Are there any programs like that?
12 Answers
The package.json file is used by npm to learn about your node.js project.
Use npm init
to generate package.json files for you!
It comes bundled with npm. Read its documentation here: https://docs.npmjs.com/cli/init
Also, there's an official tool you can use to generate this file programmatically: https://github.com/npm/init-package-json
First off, run
npm init
...will ask you a few questions (read this first) about your project/package and then generate a package.json file for you.
Then, once you have a package.json file, use
npm install <pkg> --save
or
npm install <pkg> --save-dev
...to install a dependency and automatically append it to your package.json
's dependencies
list.
(Note: You may need to manually tweak the version ranges for your dependencies.)
I just wrote a simple script to collect the dependencies in ./node_modules. It fulfills my requirement at the moment. This may help some others, I post it here.
var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
main();
In my case, the above script outputs:
"colors": "0.6.0-1",
"commander": "1.0.5",
"htmlparser": "1.7.6",
"optimist": "0.3.5",
"progress": "0.1.0",
"request": "2.11.4",
"soupselect": "0.2.0", // Remember: remove the comma character in the last line.
Now, you can copy&paste them. Have fun!
npm init
to create the package.json file and then you use
ls node_modules/ | xargs npm install --save
to fill in the modules you have in the node_modules folder.
Edit: @paldepind pointed out that the second command is redundant because npm init
now automatically adds what you have in your node_modules/ folder. I don't know if this has always been the case, but now at least, it works without the second command.
Command line:
npm init
will create package.json file
To install , update and uninstall packages under dependencies into package.json file:
Command line :
npm install <pkg>@* --save
will automatically add the latest version for the package under dependencies into package.json file
EX:
npm install node-markdown@* --save
Command line:
npm install <pkg> --save
also will automatically add the latest version for the package under dependencies into package.json file
if you need specific version for a package use this Command line:
npm install <pkg>@<version> --save
will automatically add specific version of package under dependencies into package.json file
EX:
npm install [email protected] --save
if you need specific range of version for a package use this Command line:
npm install <pkg>@<version range>
will automatically add the latest version for the package between range of version under dependencies into package.json file
EX:
npm install koa-views@">1.0.0 <1.2.0" --save
For more details about how to write version for package npm Doc
Command line:
npm update --save
will update packages into package.json file and will automatically add updated version for all packages under dependencies into package.json file
Command line:
npm uninstall <pkg> --save
will automatically remove package from dependencies into package.json file and remove package from node_module folder
You can now use Yeoman - Modern Web App Scaffolding Tool on node terminal using 3 easy steps.
First, you'll need to install yo and other required tools:
$ npm install -g yo bower grunt-cli gulp
To scaffold a web application, install the generator-webapp generator:
$ npm install -g generator-webapp // create scaffolding
Run yo and... you are all done:
$ yo webapp // create scaffolding
Yeoman can write boilerplate code for your entire web application or Controllers and Models. It can fire up a live-preview web server for edits and compile; not just that you can also run your unit tests, minimize and concatenate your code, optimize images, and more...
Yeoman (yo) - scaffolding tool that offers an ecosystem of framework-specific scaffolds, called generators, that can be used to perform some of the tedious tasks mentioned earlier.
Grunt / gulp - used to build, preview, and test your project.
Bower - is used for dependency management, so that you no longer have to manually download your front-end libraries.
1. Choice
If you git and GitHub user:
generate-package
more simply, than npm init
.
else
and/or you don't like package.json
template, that generate-package or npm init
generate:
you can generate your own template via scaffolding apps as generate, sails or yeoman.
2. Relevance
This answer is relevant for March 2018. In the future, the data from this answer may be obsolete.
Author of this answer personally used generate-package at March 2018.
3. Limitations
You need use git and GitHub for using generate-package.
4. Demonstration
For example, I create blank folder sasha-npm-init-vs-generate-package
.
4.1. generate-package
Command:
D:\SashaDemoRepositories\sasha-npm-init-vs-generate-package>gen package
[16:58:52] starting generate
[16:59:01] √ running tasks: [ 'package' ]
[16:59:04] starting package
? Project description? generate-package demo
? Author's name? Sasha Chernykh
? Author's URL? https://vk.com/hair_in_the_wind
[17:00:19] finished package √ 1m
package.json
:
{
"name": "sasha-npm-init-vs-generate-package",
"description": "generate-package demo",
"version": "0.1.0",
"homepage": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package",
"author": "Sasha Chernykh (https://vk.com/hair_in_the_wind)",
"repository": "Kristinita/sasha-npm-init-vs-generate-package",
"bugs": {
"url": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package/issues"
},
"license": "MIT",
"engines": {
"node": ">=4"
},
"scripts": {
"test": "mocha"
},
"keywords": [
"generate",
"init",
"npm",
"package",
"sasha",
"vs"
]
}
4.2. npm init
D:\SashaDemoRepositories\sasha-npm-init-vs-generate-package>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (sasha-npm-init-vs-generate-package)
version: (1.0.0) 0.1.0
description: npm init demo
entry point: (index.js)
test command: mocha
git repository: https://github.com/Kristinita/sasha-npm-init-vs-generate-package
keywords: generate, package, npm, package, sasha, vs
author: Sasha Chernykh
license: (ISC) MIT
About to write to D:\SashaDemoRepositories\sasha-npm-init-vs-generate-package\package.json:
{
"name": "sasha-npm-init-vs-generate-package",
"version": "0.1.0",
"description": "npm init demo",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Kristinita/sasha-npm-init-vs-generate-package.git"
},
"keywords": [
"generate",
"package",
"npm",
"package",
"sasha",
"vs"
],
"author": "Sasha Chernykh",
"license": "MIT",
"bugs": {
"url": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package/issues"
},
"homepage": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package#readme"
}
Is this ok? (yes) y
{
"name": "sasha-npm-init-vs-generate-package",
"version": "0.1.0",
"description": "npm init demo",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Kristinita/sasha-npm-init-vs-generate-package.git"
},
"keywords": [
"generate",
"package",
"npm",
"package",
"sasha",
"vs"
],
"author": "Sasha Chernykh",
"license": "MIT",
"bugs": {
"url": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package/issues"
},
"homepage": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package#readme"
}
I think, that generate-package
more simply, that npm init
.
5. Customizing
That create your own package.json
template, see generate and yeoman examples.
npm link
– Philipp Kyeck