Used from yarn introduction for Node.js in WSL environment

Introduction

It is mainly used as a memorandum for posters. As a Node.js package manager useful for creating the web Introduced yarn. ([Introduction of Node.js is here] nodejs) (Pip-like feeling in Python) You can use yarn for your own programming, such as various libraries.

environment

Install on wsl (Windows Subsystem for Linux).

Editor: VSCode
Shell : bash version 4.4.20
Ubuntu: 18.04.4 LTS
node  : v10.14.2

table of contents

  1. ** Introduction of yarn ** [>>](Introduction of # 1 yarn)
  2. ** About yarn ** [>>](About # 2 yarn)
  3. ** Create npm package ** [>>](# 3 Create npm package)
  4. ** Use of created package ** [>>](# 4 Use of created package)

1. Introduction of yarn

Open the wsl console and

wsl


$ curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.21.1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     

Omission
> Successfully installed Yarn 1.21.1! Please open another terminal where the 
`yarn` command will now be available.

Installation is complete when the last line is displayed.

Then load the .bashrc file or restart VS Code.

wsl


$ source ~/.bashrc

2. About yarn

For detailed usage (CLI), please see [Official Site] yarncli.

To install the npm package with yarn, Global installation: Used throughout the Node.js runtime environment Local installation: Use only in the current directory There are two types, but this time I will try all local installation Please understand.

3. Create npm package

This package is a package that outputs the Fibonacci sequence and the Tribonacci sequence. I don't know what kind of situation it will be useful in. It ’s just a practice of making a package.

〇 Create a directory to create a package

wsl


$ mkdir trib
$ cd trib 

Work in the trib directory below.


〇 Tutorial for creating npm packages

There are no particular changes this time.

wsl


$ yarn init
yarn init v1.21.1
question name (trib):

I pressed ʻEnter and changed the package name to trib`.

question version (1.0.0):

I pressed ʻEnter to change the version to 1.0.0`.

question description:

I pressed ʻEnter` and didn't add a description of the package.

question entry point (index.js):

Press ʻEnter to name the file to be loaded as library I made it ʻindex.js.

question repository url:

Press ʻEnter` to not publish the Git repository.

question author:

I pressed ʻEnter` and didn't set the author name for npm.

question license (MIT):

Press ʻEnter to comply with the MIT License`.

question private:

Press ʻEnter` and

success Saved package.json
Done in 3.45s.

You now have a file called package.json with the contents of the package written in it.

wsl


$ ls
package.json
$ cat package.json
{
  "name": "trib",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT"
}

〇 Implementation of package trib

Create ʻindex.js in the trib` directory by any method (such as touch), We will actually implement it.

index.js


'use strict';
//Fibonacci sequence
function printFib(length) {
    console.log(length+'Fibonacci sequence up to the third');
    const memo = new Map;
    memo.set(0, 0);
    memo.set(1, 1);
    function fib(n) {
        if (memo.has(n)) {
            return memo.get(n);
        }
        const value = fib(n - 1) + fib(n - 2);
        memo.set(n, value);
        return value;
    }
    for (let i = 0; i < length; i++) {
        console.log(fib(i));
    }
}

//Tribonacci sequence
function printTrib(length) {
    console.log(length+'Tribonacci sequence up to the third');
    const memo = new Map();
    memo.set(0, 0);
    memo.set(1, 0);
    memo.set(2, 1);
    function trib(n) {
        if (memo.has(n)) {
            return memo.get(n);
        }
        const value = trib(n - 1) + trib(n - 2) + trib(n - 3);
        memo.set(n, value);
        return value;
    }
    for (let i = 0; i < length; i++) {
        console.log(trib(i));
    }
}
module.exports = { printFib, printTrib };

Aside from the contents of the function ... (sorry)

module.exports = { printFib, printTrib };


 In this part, you will register the function as a property of the `module.exports` object.
 This is an abbreviation when the property name and the variable name of the value are the same, so
 If the property name and the variable name of the value are different,

module.exports = { printFib: printFib, printTrib: printTrib };

 Please write like.

# 4. Use of the created package
 To use (install) the package
 Create a directory. (∵ Local installation)


#### **`wsl`**
```bash

$ cd ..
$ mkdir trib-run
$ cd trib-run/

Below, work in the trib-run directory.


In the same way as before, do [Tutorial](# 〇 npm package creation tutorial).

$ yarn init
yarn init v1.21.1
The following is omitted

When you're done, install the package you just created.

wsl


$ yarn add ../trib
yarn add v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ [email protected]
info All dependencies
└─ [email protected]
Done in 0.28s.

Various messages are displayed and the installation is complete.

In the trib-run directory by any method (such as touch), such as sample.js We will create a JS file and actually use it.

sample.js


'use strict';
const algo = require('trib');

algo.printFib(10);
algo.printTrib(10)

Let's run it.

wsl


$ node sample.js
Fibonacci sequence up to the 10th
0
1
1
2
3
5
8
13
21
34
Tribonacci sequence up to 10th
0
0
1
1
2
4
7
13
24
44

From the above, we were able to install and use the package.

in conclusion

I'm still a beginner, so I wrote it to organize my mind. I will add it here as soon as the knowledge is updated. Please note that the content is thin because it is still a shallow study on Node.js and yarn. I would like to fully understand the relationship with Git.

Yarn cat is cute ... Goodbye: wave:

Related article

-[Used from the introduction of Node.js in Linux environment] nodejs

reference

-N Preparatory School 2020 Introduction to Programming -[yarn official website] yarncli

Recommended Posts

Used from yarn introduction for Node.js in WSL environment
Operate mongoDB from python in ubuntu environment ① Introduction of mongoDB
From Ubuntu 20.04 introduction to environment construction
Introduction to docker Create ubuntu environment in ubuntu
I started Node.js in a virtual environment
++ and-cannot be used for increment / decrement in python
Functions that can be used in for statements
Use Python in your environment from Win Automation
Search for yourself from methods in Django's model
[Introduction for beginners] Working with MySQL in Python
[For beginners] Introduction to vectorization in machine learning