Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
春からはじめる新しいNode.js / start new Node.js in spring
Search
Masashi Hirano
April 20, 2018
Programming
2
970
春からはじめる新しいNode.js / start new Node.js in spring
関西Node学園 梅田キャンパス 1時限目
https://nodejs.connpass.com/event/82614/
でNode.js v10やEcmaScriptの新機能の話をしました。
Masashi Hirano
April 20, 2018
Tweet
Share
More Decks by Masashi Hirano
See All by Masashi Hirano
You may not need XXX in Node.js
masashi
5
1.2k
OSSとコミュニティを支える
masashi
1
1k
英語ができなかった自分達が、グローバルチーム立ち上げに挑戦!?
masashi
1
3.1k
フロントエンド開発のためのセキュリティ入門について
masashi
1
450
フロントエンド開発のためのセキュリティ入門
masashi
49
17k
Node.jsの2022年と未来 / Node.js in 2022 and Future
masashi
1
1k
Corepack ~Node.jsに追加されたパッケージマネージャーマネージャー~ / #tng37
masashi
3
12k
Node.js + Web Compatibility
masashi
2
590
サイボウズが行うフロントエンドの品質保証 / Frontend Quality Assurance at Cybozu
masashi
10
2.9k
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
130
Arm移行タイムアタック
qnighy
0
310
Ethereum_.pdf
nekomatu
0
460
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
Realtime API 入門
riofujimon
0
150
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
Better Code Design in PHP
afilina
PRO
0
120
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
250
Featured
See All Featured
Navigating Team Friction
lara
183
14k
Unsuck your backbone
ammeep
668
57k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Designing for humans not robots
tammielis
250
25k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Making Projects Easy
brettharned
115
5.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Transcript
य़͔Β͡ΊΔ ৽͍͠Node.js ؔNodeֶԂ #1 ฏ ণ࢜ / @shisama
{ "about": { "name": "Masashi Hirano", "works": "Weblio, Inc.", "twitter":
"@shisama_", "github": "shisama" } }
Agenda • ݱࡏͷNode.jsΛ෮श • ৽͍͠Node.jsΛ༧श • ৽͍͠Node.jsΛ࣮श
ରͱ͢Δਓ • Node.jsΛͬͨ͜ͱ͋Δਓ • Node.jsͬͯΔ͚Ͳɺ࠷৽ใΛΓ͍ͨਓ • ݹ͍όʔδϣϯͷNode.jsͬͯΔਓ
ࠓ͞ͳ͍͜ͱ • Node.jsͱ • ϑϩϯτΤϯυ(jQuery,React,Angular,Vue…) • ػೳ༷ͷͯ͢ʢ࣌ؒత੍ʣ
Goal • Node.jsͷ࠷৽ใΛΔ • ݹ͍ڥΛΞοϓσʔτ͢Δ • ৽͍͠Node.jsͷػೳΛͬͯΈΔ
·ͣࠓճ͢༰ʹؔ͢Δ ༻ޠΛͬ͘͟Γ෮श
V8 engine • Google͕։ൃ͢ΔJavaScriptΤϯδϯ • ChromeͱNode.jsͰΘΕ͍ͯΔ • ࣮ߦ͕͍
npm • Node Package Manager • ϥΠϒϥϦཧ • λεΫ࣮ߦ
V8ɺnpmͷόʔδϣϯใ https://nodejs.org/en/download/releases/
EcmaScript • JavaScriptͷ༷ • ҰൠతʹEcmaScriptECMA-262Λࢦ͢ • TC39ͱ͍͏ҕһձ͕༷ࡦఆΛ͍ͯ͠Δ • Living Standard
• ৗʹ༷͕Ξοϓσʔτ͞ΕΔ
EcmaScript • 2ϲ݄ʹ1ճϛʔςΟϯά͕ߦΘΕ͍ͯΔ • ֤ػೳͷεςʔδߋ৽ͳͲ͕ٞ͞ΕΔ • stage-4ʹͳͬͨͷ͕1ʹ1ճEcmaScript ͷ༷ͱͯ͠ϦϦʔε͞ΕΔ(ES201X)
EcmaScript • ༷ࡦఆ·Ͱʹstage0ʙstage4·Ͱͷஈ֊͕ଘࡏ͢Δ • 0 Strawman - ΞΠσΞ • 1
Proposal - ఏҊ • 2 Draft - υϥϑτ • 3 Candidate- ༷ॻͱಉ͡ܗࣜ • 4 Finished - ࡦఆྃ
EcmaScript https://azu.github.io/slide-what-is-ecmascript/slide/ B[V͞Μͷʰ&$."4DSJQUͱԿ͔ʁʱ͕ Θ͔Γ͍͢ͷͰಡΜͰΈͯ΄͍͠
&DNB4DSJQUͷ࠷৽ใ5$ͷ ϦϙδτϦʹ͋Γ·͢ʂ
ݱࡏͷNode.jsΛ෮श
Release Schedule • github.com/nodejs/Releaseʹࡌ͍ͬͯΔ • ʹҰճϝδϟʔϦϦʔε͕ߦΘΕΔ
https://github.com/nodejs/Release
https://github.com/nodejs/Release
Active • LTS (Long Term Support) • 18ϲ݄ؒαϙʔτ͕ߦΘΕΔ • ຖ10݄͔ΒLTS
ActiveʹͳΔ • όʔδϣϯ൪߸͕ۮ
https://github.com/nodejs/Release
Current • ࠷৽ͷϦϦʔε൛ • ϝδϟʔόʔδϣϯ͕حˠ10݄ʹϦϦʔε • ϝδϟʔόʔδϣϯ͕ۮˠ4݄ʹϦϦʔε
https://github.com/nodejs/Release
Maintenance • LTSCurrentظؒऴྃޙͷϝϯςφϯεظؒ • LTS12ϲ݄ؒαϙʔτ • όάɺ੬ऑੑɺυΩϡϝϯτͳͲ͕ϝϯς͞ ΕΔ
https://github.com/nodejs/Release ݱࡏαϙʔτதͷόʔδϣϯʁ
Node 4.x • V8Τϯδϯ4.5 • ES2015ͷػೳͷଟ͕͑͘ΔΑ͏ʹ • 2018/04/30ʹϝϯςφϯεऴྃ༧ఆ • αϙʔτࢭ·ΔͷͰΞοϓσʔτඞਢ
Node 6.x • V8Τϯδϯ5.1 • ProxyσϑΥϧτҾ͕͑ΔΑ͏ʹ • 2018/04/30͔ΒLTSͰͳ͘ͳΔ༧ఆ • ϝϯςφϯε͞ΕΔ͚Ͳདྷ4݄ʹαϙʔτ
ऴྃ༧ఆ
Node 8.x • ݱࡏͷLTS • V8Τϯδϯ6.1 • async / awaitΛαϙʔτ͢Δ࠷ॳͷLTS
• util.promisify, inspectorͳͲ͕Ճ͞Εͨ
Node 8.x • npm@5 • package-lock.json • 8.5͔Βϑϥά͖ͰES Modules͑Δ •
import / export syntax • AWS LambdaͰ࠷ۙΑ͏͘v8͕͑ΔΑ͏ʹ
Node 8.x • 2019/04͔ΒLTSͰͳ͘ͳΔ • 2019/12ʹϝϯςφϯεऴྃ • ࠓ͏ͳΒ͜ͷόʔδϣϯ͕ແ
Node 9.x • ݱࡏͷCurrent • V8Τϯδϯ6.2 • HTTP2͕ϑϥάͳ͠Ͱ͑Δ • 2018/06/30Ͱϝϯςφϯεऴྃ༧ఆ
͜͜·Ͱͷ͓͞Β͍ • 4.xͬͯΔਓࠓ݄தʹΞοϓσʔτ • 6.xͬͯΔਓ̍ҎʹΞοϓσʔτ • 8.xͬͯΔਓ·ͩ̍Ҏ্େৎ • 9.xͬͯΔਓ݄̒·ͰʹΞοϓσʔτ
͜͜·Ͱͷ͓͞Β͍ • 4.xͬͯΔਓࠓ݄தʹΞοϓσʔτ • 6.xͬͯΔਓ̍ҎʹΞοϓσʔτ • 8.xͬͯΔਓ·ͩ̍Ҏ্େৎ • 9.xͬͯΔਓ݄̒·ͰʹΞοϓσʔτ ࠓ͕Ξοϓσʔτ͢Δͱ͖Ͱ͢Αʂʂ
৽͍͠Node.jsΛ༧श
https://github.com/nodejs/Release
None
Node.js 10.0 • 2018/04/24ʹϦϦʔε༧ఆ • ࣍ͷLTS • V8Τϯδϯ6.6 • npm@6(ϦϦʔε࣌ʹೖΔ͔ະఆ)
V8 Engine • ݱࡏͷmasterʹೖͬͯΔͷ6.6 • 6.7ʹͯ͠΄͍͠ཁ͋Δ
WWFSTJPOIʹఆٛ͞Ε͍ͯΔ https://github.com/nodejs/node/blob/master/deps/ v8/include/v8-version.h ࠷৽ͷNode.jsͷV8ʁ
V8 engine • Node 9.x(V8 6.2) => Node 10.0(V8 6.6)
• EcmaScript৽ػೳͷՃ(ޙड़) • ࣮ߦͷվળ • asyncͷύϑΥʔϚϯεվળ
https://github.com/nodejs/node/pull/19091 V8 6.7 7ʹTUBCMFʹͳΔ͚Ͳɺ ͦΕͰ͗͢Δ /PEF݄ޙʹϦϦʔε͍ͨ͠
V8 6.7 • Chrome 67 • BigInt(64bitܕ)ͳͲEcmaScript৽ػೳͷ Ճ • Node
v10Ͱϑϥά͖Ͱ͑Δͷ͋ Δ
npm@6 https://github.com/npm/npm/releases/tag/v6.0.0-next.0 มߋখ͞Ίɻ/PEF͕ϦϦʔε͞Ε Δ·Ͱখ͍͞มߋ͔͠͠ͳ͍༧ఆ
npm@6 https://github.com/npm/npm/releases/tag/v6.0.0-next.0 ࠓޙʹେ͖ͳมߋΛೖΕΔ ༧ఆɻOQN!ʹʂ
npm@6 • Node 10.0ʹ͚ͯখ͞ͳมߋ͋Δ • Node 4.xͱNode 7.xͷαϙʔτऴྃ • npm
ci && npm tͳnpm cit͕Ճ • npm install͢Δͱ͖ʹdeprecateͷόʔδϣϯ ΛճආͳͲ
deprecateͷόʔδϣϯΛճආ $ npm install example@~1.1.0 // 1.1.2 // 1.1.3 (deprecated)
// 1.2.0 (latest) ͕Yͷ࠷৽͕ͩɺEFQSFDBUFʹͳͬ ͍ͯΔͨΊΠϯετʔϧ͞Εͳ͍
EcmaScript৽ػೳ • ଟ͘ͷػೳ͕Ճ͞Εͨ • ES Modules·ͩϑϥά͚ͳ͍ͱ͑ͳ͍
Node.js v10ʹՃ͞ΕΔ EcmaScript৽ػೳ • Dynamic import • Async Iteration •
Promise.prototype.finally • RegExp New Features • Intl.NumberFormat.formatToParts • Function.prototype.toString() • Optional catch binding • String.prototype.trimStart • String.prototype.trimEnd
Dynamic Import (async() => { let count = 0; let
id = setInterval(async() => { const i = Math.floor(Math.random() * 3) + 1; const module = await import(`./hello${i}.mjs`); module.hello(); count++; if (count === 10) { clearInterval(id); } }, 1000); })(); ಈతʹϞδϡʔϧΛJNQPSUՄ ೳʹ
Async Iteration async function* gen() { while (true) { const
res = await axios.get(‘https:// hogehoge.com'); const data = await res.data; yield data; } } (async() => { for await (const data of gen()) { console.log(data); } })(); 1SPNJTFͳྻΛGPSBXBJU PGͰඇಉظϧʔϓ͕Մೳʹ
Promise.prototype.finally Promise.resolve(“then”) .then(val => { console.log(val); throw new Error(“catch") }).catch(err
=> { console.log(err.message) }).finally(() => { console.log(“finally") }); pOBMMZ ͕Ճ
RegExp New Features const result = '123$456 $789 $ 000'.match(/(?<=\
$)\d+/g); console.log(result); // ['456', '789'] ਖ਼نදݱͷޙಡΈ͕Մೳɻ ଞʹ υοτ ͕վߦจࣈʹϚον͢ ΔΑ͏ʹͳΔTϑϥάͳͲ
Intl.NumberFormat.formatTo Parts const num = 1000; const formatter = new
Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }); const yen = formatter.formatToParts(num).map(({type, value}) => { switch (type) { case 'currency': return "<strong>" + value + "</ strong>"; default: return value; } }).reduce((string, part) => {return string + part}); console.log(yen); // => "<strong>¥</strong>1,000" ͷΧελϜϑΥʔϚοτ ͕Մೳʹ
Function.prototype.toString function /* a comment */ foo () {} foo.toString();
// → 'function /* comment */ foo () {}' UP4USJOH ͷ༷มߋ
Optional catch binding try { const a = 1; a
= 2; } catch { console.error("catch"); } DBUDIͷҾͳ͠ͰՄೳʹ
String.prototype.trim{Start|End} const value = " has spaces "; console.log(`‘${value}'`); //
=> ‘ has spaces ‘ console.log(`‘${value.trimStart()}'`); // => ‘has spaces ‘ console.log(`trimEnd: '${value.trimEnd()}'`); // => ‘ has spaces‘ จࣈྻͷલޙͷεϖʔεΛআ ڈ
http://node.green/ OPEFHSFFOͱ͍͏αΠτʹ /PEFKTͷόʔδϣϯ͝ͱʹ &4ͷͲͷػೳ͕͑Δ͔ࡌ͍ͬͯΔ
ͦͷଞؾʹͳΔՃػೳ • fs͕PromiseϕʔεͰಈ͔ͤΔΑ͏ʹͳͬͨ • const data = await fs.readFile(‘./ something’,
‘utf-8’); • util.types.isPromiseͱ͔isDateͰܕఆ ͳͲ
Wʹ͍ͭͯͬͱৄ͍͠Λฉ ͖͍ͨਓདྷि౦ژʂ
৽͍͠Node.jsΛ࣮श
࠷৽ͷNode.jsΛ͏
https://github.com/nodejs/Release
master • ࠷৽ͷιʔείʔυ • ຖେྔʹίϛοτ͞Ε͍ͯΔ • ࠓϏϧυ͢ΔͱNode 10.0.0-preʹͳΔ
Node.jsΛϏϧυ https://github.com/nodejs/node/blob/master/BUILDING.md #6*-%*/(NEΛݟΕ֤04͚ͷखॱ͕ॻ ͔Ε͍ͯΔɻ Ϗϧυ࣌ؒͦΜͳʹ͔͔Βͳ͍
Node.jsΛϏϧυ $ ./configure $ make -j4 Ϗϧυதɾɾɾ $ ./node -v
> v10.0.0-pre ͜Ε͚ͩ
Ϗϧυͨ͠Node.jsΛ͏ • ৽ػೳΛࢼͯ͠ΈΔ ➡ ࠓհͨ͠ES৽ػೳಈ͘ʂʂ • Nodeຊମͷ࣮Λมߋͯ͠ಈ͔ͯ͠ΈΔ
͞Βʹ৽͍͠ػೳΛࢼ͢ • Node.jsʹ࣮ݧతʹ࣮͞Ε͍ͯΔػೳ͕͋ Δ • ϑϥάΛ͚ͭΔ͜ͱͰ͑Δ • ES Modules·࣮ͩݧతͳػೳ
ES Modules $ node —experimental-modules index.mjs FYQFSJNFOUBMNPEVMFTϑϥάΛ͏ɻ ֦ுࢠNKT
harmony • --harmonyϑϥάΛ࣮͑ݧతͳػೳ͕͑ ΔΑ͏ʹͳΔ • Node.jsʹ࣮͞Ε͍ͯΔEcmaScriptͷػೳʹ ΑͬͯϑϥάมΘΔ • EcmaScriptͷػೳ̏ͭʹ͚ΒΕΔ
EcmaScriptͷػೳ • shipping: ϦϦʔεࡁ • ϑϥάͳ͠Ͱ࣮ߦՄೳ • staged: ΄΅͕ͩɺ҆ఆ͍ͯ͠ΔͱΈͳ͞Εͳ͍ •
--harmonyϑϥάͰ࣮ߦՄೳ • in progress: ։ൃத • --harmony-class-fieldsͷΑ͏ʹݸʑʹ༻ҙ͞Εͨ harmonyϑϥά
harmony ֬ೝํ๏ $ node —v8-options | grep harmony --es_staging (enable
test-worthyɾɾɾ --harmony (enable all completedɾɾɾ --harmony_shipping (enable allɾɾɾ --harmony_array_prototype_values (ɾɾ --harmony_function_sent (enableɾɾɾ ͲΜͳIBSNPOZϑϥά͕͋Δ͔W PQUJPOTΛ࣮ߦ͢Ε֬ೝͰ͖Δ
harmony ྫ $ node —harmony-bigint > typeof 123n ‘bigint’ >
123n + 1n 124n IBSNPOZϑϥάΛ͚ͭΔ͜ͱͰ#JH*OU ͑Δ /PEFW
Node.jsʹ࣮͞Ε͍ͯͳ͍ػೳΛ͏ • BabelͰτϥϯεύΠϧ • EcmaScriptશػೳΛΧόʔ͍ͯ͠ΔΘ͚Ͱͳ ͍ • Node.jsͱޓੑ͕ͳ͍SyntaxͰಈ͘ͷͰҙ • import
{readFile} from ‘fs’; ͜ΕNode.jsͰ ErrorʹͳΓ·͢
Ξτϓοτ͢Δ • ৽͍ٕ͠ज़ʹ৮ΕͨΒ͔ͤͬ͘ͳͷͰݟΛ ڞ༗ͯ͠΄͍͠ʂ • ϒϩά • Qiita • ษڧձ
ؔNodeֶԂ ͍ͭͰ͋ͳͨͷొஃΛ ͓͍ͪͯ͠·͢ʂ
·ͱΊ • Node.jsͷαϙʔτظؒΛҙࣝ͠Α͏ʂ • Node.js v10 དྷिϦϦʔεʂ(༧ఆ) • ٕज़Δ͚ͩͰͳ͘ɺͬͯͳΜ΅Ͱ͢ʂ •
ొஃͯ͘͠ΕΔํ͓͍ͪͯ͠·͢ʂʂ
Special Thanks! ओ࠵ऀϝϯόʔ (@sbntaminif, @vanx2, @mdaisuke, @kamiyam,@leichtgewicht), ͘͞ΒΠϯλʔωοτ ༷, εϐʔΧʔͷํʑ(@mochiya98,
@sota1235, @h_michael_z) and ࠓདྷͯ͘Εͨօ༷!! 1ճ։࠵Ͱ͖ͯخ͍͠Ͱ͢ʂ͜Ε͔ΒؔNode ֶԂΛӬ͘ΑΖ͓͘͠ئ͍͍ͨ͠·͢
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠