New to Monorepo, write down some of the script commands used so far.
Assume that the final project structure is as follows:
My_project │ ├ ─ ─ node_modules ├ ─ ─ package. The json ├ ─ ─ packages │ ├ ─ ─ a │ ├ ─ ─ b │ └ ─ ─ cCopy the code
Note: All of the following commands are executed in the my_project directory.
Note: Workspace is new to NPM v7.x, so make sure your NPM version is greater than or equal to 7.0
1. Create projects
- create
my_project
directory - Open the terminal in this directory and execute
$ npm init -y
Copy the code
Note: -y means to skip the command query and create the project directly. And the same thing happens with -y.
2. Create module packages A, B, and C
Execute commands separately (create one by one) :
$ npm init -w packages/a -y
$ npm init -w packages/b -y
$ npm init -w packages/c -y
Copy the code
You can also use a simple command:
$ npm init -w packages/a -w packages/b -w packages/c -y
Copy the code
3. Install dependencies for a module package
Install axios dependency on module A:
$ npm install axios --workspace a
# can also be shortened to
$ npm i axios -w a
Copy the code
4. Install a dependency for all modules
Install dayJS dependencies for a, B and C module packages
# Notice the multiple 's' in workspaces
$ npm install dayjs --workspaces
# can also be shortened to
$ npm i dayjs -ws
Copy the code
5. Set module B as a dependency of module A
Make sure that NPM install is executed before using b modules as a dependency of A. Then you can use B modules directly in A. For example:
// a js file in module A uses module B
const b = require('b')
Copy the code
I don’t have a way to write dependencies in package.json via NPM.
But it can be done with YARN. It can be done through:
$ yarn workspace a add link:packages/b
Copy the code
After executing the command above, the dependencies in the final package.json are:
{
"dependencies": {
"b": "link:packages/b"}}Copy the code
6. Remove dependencies
Remove axios dependencies from module A:
$ npm un axios -w a
Copy the code
7. Execute scripts in the module
Let’s assume that the scripts field in the package.json file in module A is:
{
"scripts": {
"dev": "node index.js"}}Copy the code
To start the dev script in the a module package, run the following command
$ npm run dev -w a
Copy the code