Npm packages

Hi there i am trying to play around with packages and installed the date-fns package but cant seem to use it, i have a feeling its because of how my folders are set up but dont know the right way, where should my node modules folder and json package folders be in relation to my folder with all the projects im working on be? my projects are in a folder called projects on my desktop

When you want to initialise a project to use packages like that. You need to open a terminal and navigate to the directory you want to initialize. Then you need to type npm init. This will help you set up the project correctly which seems to be what you are trying to do manually. From there you can run npm install date-fns --save. This will install the package and it’s dependencies into your newly created node_modules folder and update your package.json file to reflect the changes.

You may also be interested in installing packages contained in a repository you cloned. Since most people don’t want to push the contents of node_modules to a source control repository like GitHub they instead just push the package.json file. Later, when you clone the project you can simply use npm install which will look at the package.json file and install the packages it references.

This is one of those things than when you know it, it seems simple. But when you don’t it can be so annoying. Hope you manage to sort this out!

1 Like

Could you clarify what the use of npm init is if you do not intend to publish a package to npm?

It initializes the project with the various files required in order to track packages within it. What that essentially does is create a package.json file for you with some sensible defaults.

Take a look at this. The process for using npm init in an empty folder. As you can see, all it does is add a new package.json file.

If you want to learn more about npm init and the other commands you can use, theie docs can be found here: npm-init | npm Docs

1 Like

As soon as you install a node module, a package.json is created in your root folder. So I still do not understand what the benefit of npm init is unless I plan to publish a package myself. Is there any benefit for a project that is not supposed to be deployed as a module itself?

All the steps can be accomplished manually. npm init is just a quick way to get started when you are setting up a new project and want some sensible defaults but it is by no means required.

1 Like