Home > Software engineering >  Must i specify dependencies in package json file
Must i specify dependencies in package json file

Time:02-28

I just recently heard of the package.json file but all my small projects have always worked without it. I've installed many npm modules globally and always use var x = require("express");"just for example" without even initializing the package.json and putting express as a dependency.

Is it really important

CodePudding user response:

First of all, I strongly doubt require("express") will work out of the box without having the express package installed in your project's node_modules folder, but just globally. There are ways to accomplish this, though.

But even if you accomplished to require packages from the global installation folder, what happens if you have multiple packages requiring different versions of the same package? There may be breaking changes between major versions of packages. So if you install a new version of package xy globally, older projects of yours expecting a different version of package xy may stop working.

On just your local machine, you can achieve all that, still without a package.json though.

The main purpose of the package.json comes clear, when you want to share your project with other people. Aside from some basic information (like a package name and some description), it will also list the dependencies which need to be installed for the project to be runable. While the necessary dependencies may exist on your machine, how will you make sure, they are also installed on a different machine without having them listed somewhere? And the place for listing the dependencies of a package is -- surprise surprise --- the package.json file ...

CodePudding user response:

They are global, not in your project so when you do the deploy, you will must have to install all global for each server.

Yuu can install packages-cli global, but project dependencies ( also dev on dev dependencies) is better have its own package.json so you can deploy.

Also if you share your project, how someone will know what packages is needed.

The better is to have for each project its own package.json on its root folder, even if you always use the same libs.

  • Related