• בלוג
  • עמוד 3
  • אנחנו ב 2024, ו node.js עדיין מתנהג כאילו זה 2018

אנחנו ב 2024, ו node.js עדיין מתנהג כאילו זה 2018

נוד הוא כבר מזמן לא סביבת הריצה היחידה בצד שרת להפעלת קוד JavaScript, אבל גם גירסאות עדכניות שלו ממשיכות להציג בעיות שמזמן היו צריכות להיפתר. הנה כמה דוגמאות מהסיבוב האחרון שלי איתו.

1. תוכניות של קובץ אחד

אנחנו ב 2024 ועדיין אין פיתרון ליצירת סקריפט בקובץ אחד שתלוי בתוכניות אחרות. ב deno אני יכול לכתוב:

import cowsay from 'npm:cowsay@1.6.0';

console.log(cowsay.say({
    text : "I'm a moooodule",
    e : "oO",
    T : "U "
}));

בקובץ אחד ואז להריץ אותו והכל פשוט עובד. ב node צריך להוסיף לתיקיה קובץ package.json ולהכריח את כולם להריץ npm install.

2. טייפסקריפט

אנחנו ב 2024 ועדיין אני צריך להפעיל tsc בתיקיה לפי שיכול להריץ קבצי TypeScript (כי node מריץ רק JavaScript), אבל אפילו זה לא מספיק כי אם לא תדייק בקבצי ה package.json וה tsconfig.json דברים פשוטים לא יעבדו. שוב להשוואה זה עובד לי ב deno בתור קובץ יחיד בתיקיה:

const res = await fetch('http://api.open-notify.org/astros.json');
const data = await res.json();
console.log(`There are ${data.number} astronauts currently in space`);

אבל ב node אני צריך את שני קבצי ההגדרות ו ts-node בכלל לא מוכן להריץ כזה קובץ גם כש tsc כן מצליח לקמפל אותו.

3. הפעלת קוד מהאינטרנט בלי לשאול

אני די בטוח שפעם הסכמנו שהקונספט הזה שגוי, אבל איכשהו npm עדיין מריץ המון קוד מהאינטרנט בלי לשאול בכל התקנה של ספריה, או בכל הפעלה של npx.

כך עם npx אני כותב פשוט:

$ npx cowsay hello world
 _____________
< hello world >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

וכן הסקריפט cowsay מגיע מהרשת ויכול לעשות מה שהוא רוצה על המחשב שלי.

ב deno אותו משחק נראה ככה:

$ deno run npm:cowsay Hello world

אבל הפלט שונה - במקום להריץ את הסקריפט יש קודם כל רצף של שאלות:

┌ ⚠️  Deno requests read access to <CWD>.
├ Requested by `Deno.cwd()` API.
├ Run again with --allow-read to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all read permissions) >

✅ Granted read access to <CWD>.
┌ ⚠️  Deno requests env access to "_".
├ Run again with --allow-env to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions) >

✅ Granted env access to "_".
┌ ⚠️  Deno requests read access to <main_module>.
├ Requested by `Deno.mainModule` API.
├ Run again with --allow-read to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all read permissions) >

✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
┌ ⚠️  Deno requests env access to "SHELL".
├ Run again with --allow-env to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions) >

✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
✅ Granted env access to "SHELL".
┌ ⚠️  Deno requests read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/package.json".
├ Requested by `Deno.statSync()` API.
├ Run again with --allow-read to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all read permissions) >

✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
✅ Granted env access to "SHELL".
✅ Granted read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/package.json".
┌ ⚠️  Deno requests env access to "LC_ALL".
├ Run again with --allow-env to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions) >

✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
✅ Granted env access to "SHELL".
✅ Granted read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/package.json".
✅ Granted env access to "LC_ALL".
┌ ⚠️  Deno requests env access to "LC_MESSAGES".
├ Run again with --allow-env to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions) >

✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
✅ Granted env access to "SHELL".
✅ Granted read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/package.json".
✅ Granted env access to "LC_ALL".
✅ Granted env access to "LC_MESSAGES".
┌ ⚠️  Deno requests env access to "LANG".
├ Run again with --allow-env to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions) >

✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
✅ Granted env access to "SHELL".
✅ Granted read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/package.json".
✅ Granted env access to "LC_ALL".
✅ Granted env access to "LC_MESSAGES".
✅ Granted env access to "LANG".
┌ ⚠️  Deno requests env access to "LANGUAGE".
├ Run again with --allow-env to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all env permissions) >

✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
✅ Granted env access to "SHELL".
✅ Granted read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/package.json".
✅ Granted env access to "LC_ALL".
✅ Granted env access to "LC_MESSAGES".
✅ Granted env access to "LANG".
✅ Granted env access to "LANGUAGE".
┌ ⚠️  Deno requests read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/cows/default.cow".
├ Requested by `Deno.readFileSync()` API.
├ Run again with --allow-read to bypass this prompt.
└ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all read permissions) >


✅ Granted read access to <CWD>.
✅ Granted env access to "_".
✅ Granted read access to <main_module>.
✅ Granted env access to "SHELL".
✅ Granted read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/package.json".
✅ Granted env access to "LC_ALL".
✅ Granted env access to "LC_MESSAGES".
✅ Granted env access to "LANG".
✅ Granted env access to "LANGUAGE".
✅ Granted read access to "/Users/ynonp/Library/Caches/deno/npm/registry.npmjs.org/cowsay/1.6.0/cows/default.cow".
 _____________
< Hello world >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

אז נכון לאף אחד אין כח לאשר כל הרשאה שאפליקציה צריכה, אבל עם האינטרנט של היום אי אפשר להיות זהירים מדי.