Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/logreg-n-coffee/nodejs-minimal-setup
Boilerplate for a Node.js application
https://github.com/logreg-n-coffee/nodejs-minimal-setup
javascript mocha nodejs npm testing
Last synced: 20 days ago
JSON representation
Boilerplate for a Node.js application
- Host: GitHub
- URL: https://github.com/logreg-n-coffee/nodejs-minimal-setup
- Owner: logreg-n-coffee
- Created: 2022-07-06T01:16:55.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-07-29T22:04:39.000Z (over 2 years ago)
- Last Synced: 2024-12-01T05:51:03.208Z (27 days ago)
- Topics: javascript, mocha, nodejs, npm, testing
- Language: JavaScript
- Homepage: http://hiruihu.com/nodejs-minimal-setup/
- Size: 108 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Node.js Project Initialization
If there is no `node_modules/` directory in the root folder, run `npm install`.
To run the project, run:
```bash
npm start
```To test the features, run:
```bash
npm test
```***
## initialization
In the terminal environment, type the following command to initialize the project:
```bash
npm init -y
```-y shorthand flag: it should take all the defaults
## configuration
### package.json
After initialization, the project folder should contain a file named `package.json`.
```bash
npm config listnpm set init.author.name ""
npm set init.author.email "[email protected]"
npm set init.author.url "example.com"
npm set init.license "MIT"
```### index.js
In the project's root folder, create a folder name `src`, and then in the `src` folder, create a file named `index.js`.
```javascript
console.log("node.js is running");
```### nodemon
Nodemon helps with creating an always-running node process. Use the following command to install the package nodemon:
```bash
npm install nodemon --save-dev
```Now package.json looks like this:
```json5
{
...
"main": "index.js",
"scripts": {
"start": "nodemon src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
...
}
```### Babel
Recent JavaScript language features (ECMAScript) are not included in the recent Node.js versions. Babel helps to transpile the code into Vanilla JavaScript. To install Babel, simply run:
```bash
npm install @babel/core @babel/node --save-dev
```Next, add the npm start script to `package.json`:
```json5
{
...
"main": "index.js",
"scripts": {
"start": "nodemon --exec babel-node src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
...
}
```After editing the `package.json` file, add the preset to the application configuration.
```bash
npm install @babel/preset-env --save-dev
```In the project's root directory, create a `.babelrc` file with the configuration:
```json5
{
"presets": [
"@babel/preset-env"
]
}
```### dotenv
In the project's root directory, create a `.dotenv` file with any environmental variables:
```bash
MY_SECRET=hereismysecret
```Then, in the terminal, install dotenv:
```bash
npm install dotenv --save
```Import dotenv into `src/index.js` to use environment variables:
```javascript
import 'dotenv/config';console.log('nodemon src/index.js');
console.log(process.env.MY_SECRET);
```### Mocha and Chai for testing
Mocha will be our test runner which is responsible for encapsulating our tests in test suites (describe-block) and test cases (it-block). Chai will be our assertion library to run equality checks or other test related scenarios.
To install Mocha:
```bash
npm install @babel/register --save-dev
``````bash
npm install mocha --save-dev
```Then, in the `package.json` file, include a test script:
```json5
{
...
"main": "index.js",
"scripts": {
"start": "nodemon --exec babel-node src/index.js",
"test": "mocha --require @babel/register 'src/**/**spec.js'"
},
"keywords": [],
...
}
```As we use `**` in between, Mocha will run recursively through the src/ folder to find all files in the application.
To install Chai, run:
```bash
npm install chai --save-dev
```Later, we can specify the first test file `src/spec.js`:
```javascript
import { expect } from 'chai';describe('true or false', () => {
it('true is true', () => {
expect(true).to.eql(true);
});it('false is false', () => {
expect(false).to.eql(false);
});
});
```Note that there are other packages for testing, such as sinon.