Best workflow using node.js npm and git

I'm going to make a big project with node.js and am currently trying to figure out a few things.

In previous node projects, I had an extra folder for all node modules that I used. This folder was ignored by git, and I managed versions and updates via git subodules, which was not easy (no dependencies, updating to a new version was not always fun.)

I'm looking for:

npm install packagename
npm dump_modules_into_file

Thus, everyone involved in this project can do:

npm install_or_update_modules_from_file

I don't want node_modules track my git repository. Basically, I want something similar to how symonfy2 handles its bundles .

PS: I know about npm submodule packagename , but this command is not very useful because it does not install dependencies and does not update modules.

PS2: I'm ready for package.json , but it also has some disadvantages. (There are no parameters, and you need to update the module versions manually.)

+5
source share
4 answers

package.json will do what you are looking for. In your comment about passing the --mongodb:native flag, this flag is an argument to the npm command and works when using package.json in your own project. The mongodb package has an “install script” that looks for this flag in the node processing environment. If this flag is present, then it creates another process for assembly. So, if you have mongodb as a dependency in your package. Json

 { "name": "MyProject" , "description": "Test" , "version": "0.0.1" , "dependencies": { "mongodb": "*" } } 

Running npm install --mongodb:native will work.

As for the “manual update”, this is really only the first time it may take some time, and I'm sure you can write a script to create it if there are a lot of dependencies. However, it looks like you have a rather large team, and if so, the automation of package.json updates will become really ugly (think about new developers, experimental features, etc.). Accountability for broken assemblies in this part of the development cycle is not necessarily a bad idea.

Literature:

EDIT : and as Nick noted, adding the node_modules directory to .gitignore prevent any of these files from being checked in your repo

+6
source

Here is a good article that explains when you should and should not check your node_modules on git. You can answer your questions.

node_modules in git

+6
source

Afaik, the only ways to manage packages is what you described, although I'm not sure if you are not interested in wrt package.json.

If you need tight control over module versions, you can explicitly specify the version number. You can also use the >=XXX approach to automatically capture the latter (above the threshold), which is sometimes great for development purposes.

This allows your teammates to do:

 npm install . 

All dependencies listed inside the package.json file will be installed. They will be set to ./node_modules , but you can .gitignore , as you noted.

+2
source

To fully verify the package configuration and make sure that the modules behave as they will be in the final deployment, I now use the following procedure. This completely eliminates the need to hack into node_module directories or require() code, so when you go to deploy it, it only works .

For internal projects or pre-release on github, you can also set "private": true to your package.json , so npm will refuse to publish it.

  • Create a project directory under git and add all your node modules as subdirectories. Subdirectory names must match their package names. If you are working with github, you need to create a separate git repository for each module directory. They can be git submodules in your repo project. Add node_module to your .gitignore files.

  • Install a tool, such as npm-server, and run it in the project directory. Then set the npm registry to localhost, so now npm will talk to your local npm server to retrieve the packages. Anyone he finds as subdirectories that he will send. Anything he does not find will be a registry.npmjs.org proxy. $ npm set registry http://localhost:6070/ $ cd ~/projects $ npm-server

  • Launch a new shell and create a separate sandbox file $ mkdir sandbox $ cd sandbox

  • Install the application using the local registry server. Clear the local npm cache and reinstall the application. I do this on one line, so it's easy to redo it through the shell. You may want to script it.

    $ npm cache clear; sleep 3; npm uninstall -g app; sleep 3; npm install -g app

  • Check your application:

    $ app ....

  • Unregister the local npm registry when installation is complete:

    $ npm set registry http://registry.npmjs.org:80/

  • After testing is complete, you can publish your application and test the deployment with the npm server stopped.

$ cd ~/projects $ npm publish app


Instead of registering and unregistering with the server, you can simply use the localhost server to install from once: $ npm --registry=http://localhost:6070/ install app


I'm writing a forked version of npm-server , so you just do: $ npmsvr on // Registers local registry server $ npmsvr start // Start local registry server $ npmsvr off // Deregisters local registry server

0
source

Source: https://habr.com/ru/post/903693/


All Articles