Stuck in multiline edit mode when importing module in Node.js REPL - node.js

I'm on node v7.5.0. I run node and then:
> import moment from 'moment';
... moment();
...
I get stuck in multiline mode as you can see. How do I use import in Node REPL?

Node does not support the module import/export syntax yet. You will have to stick with this for now:
moment = require('moment')

Related

ts-node can't use dynamic imports or import statements while in repl or eval in the cli

I have been trying to run eval or import esm module from the repl and I just can't make it work. I have tried a few things I found on the internet.
ex: Is it possible to import a Typescript into a running instance of ts-node REPL?
Not sure I am missing something, errors I am getting are:
from REPL with dynamic imports:
var a = await import("./test").then(x=>x)
//or
var a = await import("./test").then(x=>x)
//gives
//TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
using import statements on REPL
import * as test from './src/test';
//works but then when I try to use 'test' it gives
//SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import
using on the cli
ts-node --esm -e "import * as z from './src/test'; console.log(z); export {}"
//gives
//Cannot use import statement outside a module
ts-node --esm -e "const z= import('./src/test').then(console.log);"
//gives
//TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
Any ideas what is going on?
I am using ts-node 10.9.1 globally installed in node 18.2.0

How to use shorthand es6 imports in node 10+

In node v9.1.0 the following code works when utilizing with the experimental-modules flag:
import foo from './utilities/foo'
This fails in node v10.16.3. Instead I have to do the following:
import foo from './utilities/foo.mjs
Is there a way to get the older behavior back?

TypeScript - how to import 'os' module

I'm learning Typescript. To do this, I'm building a basic utility app with Node for myself. For this app, I need to use Node's OS Module. My question is, how do I import this module?
In my Typescript file, I have the following:
import { os } from 'os';
This line generates the error: "Cannot find module 'os'". What am I missing?
This line generates the error: "Cannot find module 'os'". What am I missing?
The correct code is
import os from 'os';
Also make sure you have npm i #types/node
More
Some notes I wrote on NodeJS quickstart : https://basarat.gitbook.io/typescript/docs/quick/nodejs.html
Just to update this entry:
import * as os from 'os';
and later, you can use:
const hostname = os.hostname();

Import native node modules correctly in Typescript

I've been using node for quite a while now (for my backends) and typescript with ionic (for frontend). On Ionic i realize I have managed to avoid many pitfalls and errors just because of TypeScript. I have hence decided to convert all my backends that are in pure JS to TypeScript.
The first obstacle I've run into is how to import native node modules like http, os and child_process, among others, correctly.
On most modules you can usually do something like import { some_export } from 'that_module'. I can also see the type definitions for node in the #types/ repo. I've tried import { http, os } from 'node' but I get a complaint that
/node_modules/#types/node/index.d.ts is not a module
My question hence is how should I import native node modules?
I've managed to solve this issue thanks to some light reading from this simple tutorial
To my understanding, the native modules are standalone modules that are not namespaced under node. You should therefore import from them directly.
Simply done so:
import * as http from "http";
import * as os from "os";
import * as path from "path";
.
.
.
and so on

How to force tu use RTCPeerConnection from AdapterJS

I tried to add RTCPeerConnection from adapterJS, but my typescript wouldn't allow me to do that.
Always import from lib.es6 instead off adapterJS.
I installed adapterJS via this command npm install webrtc-adapter --save
And I tried to do sth like this:
import RTCPeerConnection from 'webrtc-adapter';
or
import adapter from 'webrtc-adapter';
But it always fails.
What I'm doing wrong guys?
That was totally misunderstanding!
Adapter is working out of the box, it's the layer between my component and the javascript interpreter in browser.
So adapter is working fine, I don'w have to import them.
All I have to do is import 'webrtc'; in typings.d.ts

Resources