How to use shorthand es6 imports in node 10+ - node.js

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?

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 require a node package that wants to be a module, without type: module

I need to import a module to my project.
When I do const gifsicle = require('gifsicle'); it tells me:
ReferenceError: require is not defined in ES module scope, you can use import instead
When I try to do import, it says
SyntaxError: Cannot use import statement outside a module
Other threads say to fix this I have to add "type": "module" to my package.json
But doing so gives me this error:
ReferenceError: require is not defined in ES module scope, you can use import instead
How do I load in an ES module package into my current non-module package which uses require?
First of all add "type":"commonjs" to package.json.
Downgrade your package version. If you are using let suppose version 8.0.0 then install older version like npm i package-name#7.1.0.
That's all now enjoy coding...

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

Isomorphic import of CommonJS modules

I've written a small library that's meant to run both in the browser and on the server. The library in turn uses npm libraries published as CommonJS modules (e.g. lodash.isequal).
I'd like to compile the library two different ways:
Using tsc, generating commonjs modules for use on the server
Through webpack, using the awesome-typescript-loader, generating es6 modules for webpack to bundle
Per the docs, the correct way of importing CommonJS libraries which override module.exports is as follows:
import isEqual = require('lodash.isequal');
However for the second compilation case, typescript complains that
error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
I've managed to get code generation to work properly by importing it as:
import * as isEqual from 'lodash.isequal';
but that requires disabling typechecks, since otherwise typescript complains with errors like the following:
error TS2497: Module '"/home/user/ts-mod-test/node_modules/#types/lodash.isequal/index"' resolves to a non-module entity and cannot be imported using this construct.
Since TypeScript 2.7, this issue can be resolved by using synthetic default imports. Using the esModuleInterop flag, isEqual (and similar modules) can be imported like in babel and webpack:
import isEqual from 'lodash.isequal';
For webpack with the module option set to es6, the import will be left as-is, and webpack itself will handle the synthetic default. For node, with module set to CommonJS, the corresponding emit will be const lodash_isequal_1 = __importDefault(require("lodash.isequal"));, where the __importDefault handles the non-es module case like Babel

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

Resources