Skip to main content
Deno 2 is finally here šŸŽ‰ļø
Learn more

node from deno

  1. spawns a ā€˜nodeā€™ command in deno
  2. writes the function to the stdin
  3. gets the full stdout
  4. done!

import

import {
    f_s_version_node, 
    f_o_output_from_node_command,
    f_a_n_u8__stdout__from_node_command,
    f_s_from_node_command,
} from "./mod.js"

call a simple function in node

let s_process_plattform = await f_s_from_node_command(
    ()=>{
        console.log(process.platform) //note the console log is the 'return' value!
    }
);
console.log({s_process_plattform}) // { s_process_plattform: "linux\n" }

non primitive return values

by console.log(JSON.stringify(value)) we can stringify the value in node and parse it in denojs this way we can pass object from node to js

let o_os = JSON.parse(
    await f_s_from_node_command(
        ()=>{
            let o = require('os');
            console.log(JSON.stringify(o))
        }
    )
)
console.log(o_os)

node modules

we can also require modules in node!

let a_s_entry = JSON.parse(
    await f_s_from_node_command(()=>{

        const o_fs = require('fs');

        o_fs.readdir('./', (err, files) => {
            console.log(JSON.stringify(files))
        })
    })
)

command output

we can of course also get the typed arrays of the output we can also require modules in node!

type ā€˜moduleā€™

to run a function as a type module, we can pass a true boolean as the second argument, this will add the flag ā€˜ā€“input-type=moduleā€™ to the node command of course we will encounter the problem of ā€˜import val from ā€˜module_nameā€™ which will throw a deno parse error ā€˜error: The moduleā€™s source code could not be parsed: ā€˜importā€™, and ā€˜exportā€™ cannot be used outside of module codeā€™ this can be solved by prefixing the line with a special comment which will get replaced by an empty string ā€ and therefore will be removed in the executed nodejs code!

let a_n_u8__random = await f_a_n_u8__stdout__from_node_command(
    ()=>{
        //this_comment_will_get_removed_when_run_in_node import random from 'random'
        console.log(
            new Array(10).fill(0).map(n=>{random.int(0, 255)})
        )
    }, 
    true
)
console.log({a_n_u8__random})

non installed modules

if the node code throws an error deno will also throw an error

let s_modo_locations = 
    await f_s_from_node_command(()=>{
        const o_modo = require('modo');
        console.log((o_modo.locations))

    })
console.log({s_modo_locations})//Error: Cannot find module 'modo'