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

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

Related

Pylint complaining about function reports

I am attempting to import functions from couple of files called
mysql_calls.py
system_calls.py
If I import with
from mysql_calls import *
pylint complains about wildcard import and if I just do
import mysql_calls
pylint complains about undefined vars when I use functions from imported modules.
alert_feeder.py:215:17: E0602: Undefined variable 'mysql_query'
(undefined-variable)
What is the right way to do this? I am also not (yet?) using every functions from imported modules, and pylint also complains about that.
When you run import mysql_calls, its function mysql_query is accessible as mysql_calls.mysql_query.
More details: Modules - Python 3 documentation

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();

Importing DocumentReference in Es6

Classes like DocumentReference, CollectionReference, etc., don't seem to be exported. Is there a way to import these classes?
Install firebase if it's not in your package.json:
npm install --save firebase
In the .ts file just import whatever you need:
import * as firebase from 'firebase/app'
import DocumentReference = firebase.firestore.DocumentReference
Use DocumentReference:
posts.forEach((doc: DocumentReference) => {
doc.ref.delete()
})
using typescript, this works:
import * as firebase from "firebase"
then do
firebase.firestore().DocumentReference

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

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')

Why am I getting a "Cannot find module" err when compiling Typescript?

I am just getting started using typescript on the server and I'm pretty much stuck trying to import 3rd party npm modules. Here is what I have declared:
import mongodb = require('mongodb');
import assert = require('assert');
import Q = require('q');
... and I am getting the following errors when compiling:
src/Databse.ts(1,26): error TS2307: Cannot find module 'mongodb'.
src/Databse.ts(2,25): error TS2307: Cannot find module 'assert'.
src/Databse.ts(3,21): error TS2307: Cannot find module 'q'.
What is the correct way to import 3rd party modules?
It doesn't feel the correct way to import 3rd party's library. You can simply use only import
import "library";
OR
import {module} from "library";
Note:- when you are using above syntax then you need to make your that
the module you are importing exist otherwise you will get the error
that it can't find the module you are trying to import.
If you want to use webpack then
require("library");
Or like this
var module = require("library");
Have you installed the typing declarations ?
typings install mongodb --save
typings install dt~assert --save
typings install dt~q --save
Then you will need to reference the typings.
Either using the triple slash-directive
/// <reference path="..." />
More about it here Triple-Slash Directives
Or add the typings index.d.ts file in files array in tsconfig.json
"files": [
"./typings/index.d.ts",
]

Resources