Use of creating a package.json file in our Node project - npm

Why do we create a package.json file using npm init and what is its use while uploading our project on git?

Your package.json is the core of npm. In this file all of your dependencies and node packages are outlined. It's important to upload to you git repository so that anyone cloning your project will have the same dependencies and packages on the same version as the project was developed in. In addition, being able to startup a project with npm start and npm install makes everyone's life easier.

Related

Install Nestjs peer dependencies for development

I download source code of (Nestjs)[https://github.com/nestjs/nest] framework and try to install its dependencies by running npm ci and after that lerna bootstrap. It works fine some how. But when I run tests it fails on nest-application.spec.ts file arguing it has #nestjs/microservices dependencies which is not resolved. As I see in core package node_modules folder there is only a symlink to common package. Although in core's package.json file #nestjs/microservices is marked as peer dependencies but lerna does not put it's symlink in node_modules folder. I also though that was an issue and opened a pull request https://github.com/nestjs/nest/pull/10165 which added #nestjs/microservices as devDependencies in package.json file which got rejected by kamil. If thats not a issue how can I install peer dependencies by lerna or any other method? How do themselves develop the project? Of course I can manually symlink the folder but I'm sure kamil is not doing it this way :)

Why we are required to install libraries each time in new npm project?

Each time when I start a new project I download many dependencies and configure some settings in VS Code again and again. For example I run these commands like npm install --save-dev webpack and npm install eslint --save-dev and copy some config files like .eslintrc.
Is there any way to avoid this?
I use npm and VS code.
the possible way is to delete the new node module folder and package.json from new installed project, and copy it from the old project.
there is no legal way to perform a new project without the installation of required packages through npm.

Install other package.json dependencies

Simple question : Is it possible, in a package.json, to reference another package.json, and install its dependencies ?
Thank you.
Yes, this is possible, and this is automatically done by npm install.
If you have pkg-a that depends on pkg-b, including pkg-a in your dependencies will install both pkg-a and pkg-b when running npm install. That is because dependencies are actually references to the package.json of other packages. NPM, upon running install, builds a dependency tree of all the packages that are indirectly required by your current project, and installs all of them in the node_modules directory, and keeps track of them all in package-lock.json.
Good question! but this is not possible as you cannot internally reference one json document from another (json is just a document format, it lacks any ability to process logic, import files etc), npm is configured to run using a single package.json file so your best best would be to put all your dependencies in a single package.json file or split your project into two directories with two separate package.json files, two npm installs etc, if for some reason you require your dependencies to be separate. You could then run your two node projects separately and connect via http if you wish.
The only way that you could come close to doing this would be to write an npm start script in the package.json that cds to another directory with a package.json and runs npm install, this would however only install the dependencies in the second directory node-modules/ folder

How to install npm own local packages?

I have 2 projects(packages) in npm, I want to inject package_A as dependency to package_B. In package_A root folder, I run npm install -g, then npm install it to C:\Users\Myuser\AppData\Roaming\npm\node_moduls\package_A folder. Now in packages.json in package_B I add "package_A": "1.0.0" in dependencies. When in package_B root file I run npm install, its failed package_A#1.0.0 not found.
How can I identified npm to its my own local package?
Notes:
We are a team, then I don't want to address package_A explicitly.
We are using nexus repository manager.
I don't want to publish my projects to http://registry.npmjs.org/.
I'm not 100% clear what you have tried. If you are going to use a custom module for another application you are developing, installing globally won't do the trick. You have to publish that module in npm.
Check this link for more info on publishing in npm
If you have completed the steps correctly, and still no good happens, please check your naming of the module in package.json file.
Instead of typing in the name and version number in package.json file and then npm install, try directly installing in the terminal with --save so that it will automatically be added to package.json file with correct spelling.

How to convert NuGet package to npm

I need to convert/repackage NuGet package to npm package. Has anyone tried this before?
Is there any automatic tool that exist? (At least for simple cases with no dependencies)
You have to maintain the package contents with a package.json file in a git repo and register the repo in npm registry to publish it to npm.
UG: publishing-npm-packages