Import unqualified name in Node.js - node.js

In Python I can import the names in my module as qualified
import myModule
or unqualified
from myModule import *
In Node.js, I can import as qualified
var myModule = require('myModule')
Is there a way to import names unqualified?

You can do that in ES6: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
ES6 imports are not supported in node.js at this point though. You could get it by writing ES6 code and transpiling it with babel, for example https://babeljs.io/

Related

Directory import is not supported resolving ES modules when trying import { map } from 'rxjs/operators';

I'm getting the following error when trying to import the map operator.
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import is not supported resolving ES modules imported from
import { map } from 'rxjs/operators';
Node version: v14.15.0
It seems that ES6 modules are not yet supported
To be able to import the map module I needed to use internals as follow:
import { map } from 'rxjs/internal/operators/map.js';
Hope this might help someone.

Importing draw2d in react causes jquery reference error

As soon as I add import draw2d from "draw2d" to my imports I get ReferenceError: jQuery is not defined.
I tried installing and adding jquery but still get same error...
import React, {Component} from "react";
import jQuery from "jquery";
import draw2d from "draw2d";
Create a file with any name, e.g. import-jquery.js
import-jquery.js
import jquery from "jquery";
window.$ = window.jQuery = jquery;
App.js
import "import-jquery";
import "jquery-ui-bundle"; // you also need this
import "jquery-ui-bundle/jquery-ui.css";
import draw2d from "draw2d";
EDIT:
And don't forget to...
npm install jquery
npm install jquery-ui-bundle

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 a number of modules at once

I have a number of modules under the same folder:
/src/Web/MyLib/Types/Entity1.hs
/src/Web/MyLib/Types/Entity2.hs
/src/Web/MyLib/Types/Entity3.hs
...
Most of them require importing the same modules such as Data.Time, UUID and others. Instead of importing those models to each of the modules under /src/Web/MyLib/Types/, is there any way to create one base module, say, /src/Web/MyLib/Types/Base.hs, import all those modules (Data.Time, UUID, ...) to it and then only import Base to EntityX? I've tried it and failed. Maybe I did something wrong.
Here's an example, taken from Control.Lens, which achieves what you want: it imports everything in a base module and re-exports everything.
module Control.Lens
( module Control.Lens.At
, module Control.Lens.Cons
, module Control.Lens.Each
-- ...
) where
import Control.Lens.At
import Control.Lens.Cons
import Control.Lens.Each
-- ...

import a whole module at one time with Haskell

I'm embarrassed with a library problem with Haskell.
I finished a library made of several files
src/MyLib/Firstbib.hs
src/MyLib/Secondbib.hs
...
src/MyLib/Lastbib.hs
At this time, After cabal install I can import each file separatly with
import MyLib.Firstbib
import MyLib.Secondbib
import MyLib.Lastbib
every thing is OK
Now, I would like to import all these part of MyLib in a simple import :
import MyLib
and I can't reach to make it.
I tried to create a file named src/MyLib.hs containing :
module MyLib where
import MyLib.Types
import MyLib.Functions
import MyLib.Algo.Line
import MyLib.Algo.Point
and expose it with Cabal
Library
-- Modules exported by the library.
Hs-Source-Dirs: src
Exposed-modules: MyLib
, MyLib.Functions
, MyLib.Types
, MyLib.Algo.Line
, MyLib.Algo.Point
but it doesn't work.!
What it the correct way to import many files with only one module import (as for Gtk2Hs for example)?
This is how MyLib should look like -- maybe with different indentation:
module MyLib
(module MyLib.Types
,module MyLib.Functions
,module MyLib.Algo.Line
,module MyLib.Algo.Point
) where
import MyLib.Types
import MyLib.Functions
import MyLib.Algo.Line
import MyLib.Algo.Point
What happens is that when you put a module like that in your export list, you export all the symbols that your module knows about it.
You could potentially scope what part of this module you export, for example:
module ExampleLib
(module Data.Maybe
) where
import Data.Maybe (fromJust)
The above will just re-export fromJust from Data.Maybe, not the whole Data.Maybe module.

Resources