Original: https://blog.risingstack.com/node-js-best-practices-part-2/
Unified Style
It is necessary to create a style guide for a large team to develop JS applications. It is recommended to look at this node. js Style Guide.
Jscs is a JS code style checker. The installation commands are as follows:
NPM Install Jscs--save-dev
Next, add the custom script in the Package.json file:
Scripts: { "Jscs": "Jscs Index.js"}
Of course, you can add multiple files/folders.
Enforce Jshint/jscs Rules
Checking the code style before committing commits is a good choice.
This is easy to do, just use Pre-commit:
NPM Install--save-dev Pre-commit
Configure your package.json
file as follows:
Pre-commit ": [ " Jshint ", " Jscs "],
Before each commit, Pre-commit will find the corresponding script and run it.
JS over JSON for configuration
Many of the project's configuration files are JSON. We recommend that you also use the Config.js file:
Use Node_path
Have you ever met the following things?
When you accept the structure of a large complex project, you will find that the dependency module is very confusing. You can use the following methods to solve this problem:
- Point your moudle to the Node_modules folder
- Use
NODE_PATH
Risingstack use Node_path this way.
Setting up Node_path
Imagine the following project structure:
We can use NODE_PATH指向lib文件夹
. The Start Script section in our Package.json file can be set so that we can run our app with the NPM start command.
Dependency Injection
Dependency injection is very useful for testing
node. js Best Practices-part 2