Upgrade to Pro — share decks privately, control downloads, hide ads and more …

You may not need XXX in Node.js

You may not need XXX in Node.js

関西Node学園 11時限目 #kng11 で話しました。
https://nodejs.connpass.com/event/319446/

あくまで必要なくなるかもしれない話なので、攻撃的なコメントはお控えください。

Masashi Hirano

June 12, 2024
Tweet

More Decks by Masashi Hirano

Other Decks in Programming

Transcript

  1. You may not need XXX in Node.js @shisama_ 2024/06/12 ؔ੢NodeֶԂ

    11࣌ݶ໨ #kng11 ͜ͷ਺೥ͰNode.jsຊମ͕αϙʔτͨ͠ػೳͷ঺հ
  2. ฏ໺ণ࢜ / Masashi Hirano X: @shisama_ αΠϘ΢ζͰϚωʔδϟʔͱϑϩϯτΤϯυΤϯδχΞ Λ͍ͯ͠·͢ɻ • Node.jsίϛολʔ

    • JSConf.jpɺؔ੢NodeֶԂͳͲͷελοϑ • ʰϑϩϯτΤϯυ։ൃͷͨΊͷηΩϡϦςΟೖ໳ʱ ʢᠳӭࣾʣஶऀ
  3. fetch͕Node.jsʹ࣮૷͞Εͨ • Node.js v17.5.0Ͱ࣮ݧతʹ࣮૷͞Εͨ • Node.js v18.0.0͔Β͸ϑϥάͳ͠Ͱ࣮ߦՄೳ • Node.js v21.0.0͔Β͸࣮ݧ൛͔Β҆ఆ൛ʹঢ֨

    const res = await fetch('https://nodejs.org/api/documentation.json'); if (res.ok) { const data = await res.json(); console.log(data); }
  4. • Jest΍MochaɺVitestͳͲͱ ಉ͡ه๏ͰςετΛॻ͘͜ ͱ͕Ͱ͖Δ • ωετ͢Δ͜ͱ΋Մೳ • εΩοϓ΍Todoɺonlyɺ timeoutͷઃఆͳͲ΋Մೳ •

    v22Ͱplan()͕௥Ճ͞ΕͯΞ αʔγϣϯͷ࣮ߦճ਺΋ݕ ࠪͰ͖ΔΑ͏ʹͳͬͨ import { describe, it } from ‘node:test'; import assert from ‘node:assert'; describe('A thing', () => { it('should work', () => { assert.strictEqual(1, 1); }); it('should be ok', () => { assert.strictEqual(2, 2); }); describe('a nested thing', () => { it('should work', () => { assert.strictEqual(3, 3); }); }); it.skip(‘should be skipped', () => { assert.strictEqual(2, 2); }); it.todo(‘should be written later'); it(‘should be passed as planned', (t) => { t.plan(2); t.assert.strictEqual(2, 2); t.assert.ok(1 === 1); }); });
  5. —env- fi leϑϥάͰ࣮ߦ࣌ʹenvϑΝΠϧ͕ಡΈ ࠐΊΔΑ͏ʹ • v20.6.0Ͱ௥Ճ͞Ε࣮ͨݧతͳػೳ • v20.12.0Ͱෳ਺ߦʹରԠ • ࣮ߦ࣌ʹ—env-

    fi leͰࢦఆͨ͠ϑΝΠϧʹఆٛ͞Εͨ؀ڥม਺ΛಡΈࠐ Ή (ྫ: node —env- fi le=.env) # .env # This is a comment PORT=3000 ENV=develop
  6. —watchϑϥάͰίʔυͷมߋΛݕ஌ͯࣗ͠ಈ తʹNode.jsΛ࠶ىಈͤ͞Δ͜ͱ͕Մೳ • v16.19.0΍v18.11.0͔Βར༻Մೳ • v22.0.0Ͱ҆ఆ൛΁ঢ֨ • ࣮ߦ࣌ʹ—watchΛ͚ͭΔ • ྫ:

    node —watch-path=./src —watch-path=./tests index.js • ࣮ߦதͷNode.jsͷϓϩηεʹؔ࿈͢ΔίʔυͷมߋΛݕ஌ͯࣗ͠ಈతʹNode.jsͷϓ ϩηεΛ࠶ىಈͤ͞Δ • ։ൃதʹϓϩηεͷ࠶ىಈΛखಈͰ͢Δख͕ؒল͚ͯDX͕ྑ͍
  7. ͜Μͳײ͡Ͱimportͳ͠Ͱ࢖͑Δ const socket = new WebSocket("ws://localhost:8080"); // Connection opened socket.addEventListener("open",

    (event) => { socket.send("Hello Server!"); }); // Listen for messages socket.addEventListener("message", (event) => { console.log("Message from server ", event.data); });
  8. —allow-* ϑϥά͕ͳ͍ͱΤϥʔʹͳΔ $ node --experimental-permission t.js node:internal/modules/cjs/loader:162 const result =

    internalModuleStat(filename); ^ Error: Access to this API has been restricted at stat (node:internal/modules/cjs/loader:162:18) at Module._findPath (node:internal/modules/cjs/loader:640:16) at resolveMainPath (node:internal/modules/run_main:15:25) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/ run_main:53:24) at node:internal/main/run_main_module:23:47 { code: 'ERR_ACCESS_DENIED', permission: 'FileSystemRead', resource: '/Users/rafaelgss/repos/os/node/t.js' }
  9. fs.glob/globSync͕࣮૷͞Εͨ • v22.0.0Ͱ௥Ճ͞Εͨʢ·࣮ͩݧతͳػೳʣ • ඇಉظ൛(Callbackܗࣜ)ɺಉظ൛(globSync())ɺPromise൛͕͋Δ const { glob } =

    require('node:fs'); glob('**/*.js', (err, matches) => { if (err) throw err; console.log(matches); }); /** [ 'node22/esm.js', 'node22/glob.js', 'node22/test.js', ... ] */
  10. ESMͱCJSͷޓ׵ੑͷ޲্ • Node.js v22.0.0Ͱ࣮ݧతʹrequire()ͰESM͕ಡΈࠐΊΔΑ͏ʹͳͬͨ • —experimental-require-moduleϑϥά͕ඞཁ // esm.js export const

    foo = () => { return “bar"; }; // require.cjs const { foo } = require("./esm.js"); console.log(foo()); // bar