@@ -18,6 +18,24 @@ When developping websites or backend applications, we use Node js. You can downl
Node js dependencies are managed using a package manager (npm, yarn, pnpm...). The default package manager is `npm`.
It comes bundled with Node js.
Node dependencies are installed in the `node_modules/` folder.
Every project has a `package.json` file, which contains the project's **dependencies**, **metadata** and **scripts** shorthands.
You can use the following commands :
```bash
npm install# Install all dependencies listed in package.json
npm install <package> # Install a package and add it to package.json
npm install <package> --save-dev# Install a package as a dev dependency
# Scripts we defined in package.json
npm run build # Build the project into javascript files. The output directory is build/
npm run execute # Execute the project. The entry point is build/index.js
npm run format # Run the prettier formatter on all files (this is done automatically upon saving each file)
npm run lint # Run the eslint linter on all files (this is done automatically by VSCode when opening or changing a file)
```
### Why use (or not) typescript ?
TypeScript is a strongly typed superset of JavaScript. It is recommended to ALWAYS use TypeScript for medium to large projects, especially if multiple developers will be working on it.
...
...
@@ -72,100 +90,9 @@ You should always use a linter.
The linter is configured in the [`.eslintrc.js`](./.eslintrc.js) file.
## Environment and reproducibility
In order to make it easier for other developers to contribute to your project,
and to enable deploying your application easily, you must indicate which python version and dependencies you are using.
You typically indicate the python version in your [`pyproject.toml`](./pyproject.toml) or (`README.md`)(./README.md) file.
### Create a virtual environment
If you do not use `conda`, you may create a virtual environment. Virtual environments are often contained in a `venv` folder.
Virtual environment enable you to have separate python versions and dependencies for each project.
```bash
python --version# Ensure that your current python version is the one you want to use. Use `pyenv` to change it if needed (see wikibr).
python -m venv venv # Creates a virtual environment in the `venv` folder.
# LINUX / MACOS : activate the virtual environment
source venv/bin/activate
# WINDOWS : activate the virtual environment
venv\Scripts\activate
```
You must always activate your virtual environment before running your code or installing dependencies (else it will use your global python interpreter).
### Install and Freeze dependencies
Python depdendencies are typically indicated in a [`requirements.txt`](./requirements.txt) file.
Since python 3.10, you can use the `|` operator to indicate that a parameter can be of multiple types, and use `list` and `dict` directly as type hints.
When your code gets big, you should split it into multiple files.
Use the following test in order to run module-specific code only when you run your module directly via `python module.py`, and not when you import it from another file.