It is mainly used as a memorandum for posters. Introduce Node.js, which is useful for creating the Web.
Install on wsl (Windows Subsystem for Linux)
.
Editor: VSCode
Shell: bash version 4.4.20
Ubuntu: 18.04.4 LTS
First, install nvm
to manage the version of Node.js.
Knowing the version of Node.js you are currently using
You can switch to another version.
wsl
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
When you're done
wsl
$ source ~/.bashrc
Then, read the contents of .bashrc
.
wsl
$ nvm
Node Version Manager
Note: <version> refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`
The following is omitted. .. ..
If so, the installation is successful.
This time, I installed Node.js of Ver. 10.14.2.
wsl
$ nvm install v10.14.2
Downloading and installing node v10.14.2...
Downloading https://nodejs.org/dist/v10.14.2/node-v10.14.2-linux-x64.tar.xz...
###################################################################### 100.0%Computing checksum with sha256sum
Checksums matched!
Now using node v10.14.2 (npm v6.4.1)
Creating default alias: default -> v10.14.2
$ nvm use v10.14.2
Now using node v10.14.2 (npm v6.4.1)
$ node --version
v10.14.2
I have confirmed that the specified version of Node.js is installed.
Think of it as a Node.js version of the console.
(It feels like a Python console in Python)
You can exit the REPL twice with Ctrl
+ c
.
REPL
$ node
> 1+1
2
>
(To exit, press ^C again or type .exit)
>
I think that the way to write the program should be the same as JavaScript
.
This time I will write a simple addition program and run it.
sum.js
'use strict';
function aAdd(num) {
var res = 0;
num[0] = 0;
num[1] = 0;
for (let s of num) {
res += parseInt(s);
}
console.log(res);
}
aAdd(process.argv);
wsl
$ node sum.js 1 2 3 4
10
$ node sum.js
0
This was a program that summed the arguments.
The instructions are listed in process.argv
.
If you run $ node sum.js 1 1 1 2 3
The contents of the list are
[ '/home/yosse95ai/.nvm/versions/node/v10.14.2/bin/node',
'/home/yosse95ai/sum.js',
'1',
'1',
'1',
'2',
'3' ]
And so on.
So
num[0] = 0;
num[1] = 0;
Replace the non-numeric character string part (path part) with 0
in the part of.
I think there is probably a more clever way.
This time, it was a fairly rudimentary commentary. I'm still a beginner, so I wrote it to organize my mind. It was probably a poor sentence, but thank you for your relationship. Goodbye: wave:
-Used from yarn introduction for Node.js in Linux environment
-N Preparatory School 2020 Introduction to Programming
Recommended Posts