40

I'm running TypeScript 2.0.3, configured to with "target": "es2015",. I started with

/// <reference path="../../node_modules/@types/node/index.d.ts" />
import assert = require('assert');

But in Visual Studio, that gets flagged with a tooltip saying Import with 'require' cannot be used when targeting ECMAScript 6 or higher. I then tried:

/// <reference path="../../node_modules/@types/node/index.d.ts" />
import {assert} from 'assert';

Which generates Error TS2305 Module '"assert"' has no exported member 'assert'.

I've also tried:

/// <reference path="../../node_modules/@types/node/index.d.ts" />
import assert from 'assert';

Which generates Error TS1192 Module '"assert"' has no default export.

Burt_Harris
  • 5,336
  • 1
  • 24
  • 55
  • 1
    have you seen this already? Seems similar to what you are asking about: https://github.com/Microsoft/TypeScript/issues/9725 – Igor Oct 05 '16 at 21:33
  • Thanks, no. I'd been blocking angular out of my world (I'm an old dog.) I'll try the tsconfig.json trick from that thread though. – Burt_Harris Oct 05 '16 at 21:36
  • None of that seems to change the error messages I get for `assert`. I think the `tsconfig.json` changes from that thread mostly are an alternative to the `/// reference...` line in my example. – Burt_Harris Oct 05 '16 at 21:42
  • I recommend reading http://stackoverflow.com/questions/39415661/why-cant-i-import-a-class-or-function-with-import-as-x-from-y – Ryan Cavanaugh Oct 05 '16 at 23:14
  • Thanks @RyanCavanaughm. Does this mean that I should restore my original `import assert = require('assert')`, and ignore the fact that Visual Studio is flagging that as incompatible with ES2015? – Burt_Harris Oct 06 '16 at 14:55

3 Answers3

63

For Node 10 and above, it's better to use strict assert which can be imported as named import and renamed for convenience as assert:

import { strict as assert } from 'assert';

assert.ok(true);

assert(true);

strict is a named export from built-in assert module. Named exports avoid many of the problems mentioned in the question, problems that come from using single module.exports CommonJS export and importing it as default import. In TypeScript 2.7, --esmoduleinterop option was added to help with that.

The rest is an old answer, written in 2016:

import * as assert from 'assert';

assert.ok(true);

assert(true);

If you run typescript from the same directory where node_modules is, you don't even need to add /// <reference ...

As @Ryan Cavanaugh noted in the comment, this syntax prompts an assumption that it will work in an environment where ES6 modules are natively supported (no such environment exist yet). It is not true, it's not possible to have ES6 module that can be used as both a namespace and a function, so I think this syntax still matches the reality better:

import assert = require('assert');

but you have to use typescript options

 --target es6 --module commonjs

to get rid of Import with 'require' cannot be used when targeting ECMAScript 6 or higher error. You can also use just --target es5 option alone if that's what you need.

artem
  • 29,391
  • 6
  • 61
  • 63
  • Thanks. That does it. – Burt_Harris Oct 05 '16 at 21:44
  • Note that this will fail to actually work in a real ES6 environment. – Ryan Cavanaugh Oct 05 '16 at 23:14
  • I only get the error on `import assert = require('assert');` in Visual Studio, command line compile isn't giving me that. I have `"target": "es2015"` in my `tsconfig.json`. @RyanCavanaugh, is this message incorrect? Should I chose another `assert` package? – Burt_Harris Oct 08 '16 at 19:43
  • Thanks for the recommendation to use `strict`. People should make their code accept as little behaviour and inputs as possible. If you want to relax the rules, do it consciously and document it. – masterxilo Mar 04 '19 at 15:05
7

First, install TypeScript definitions for Node.js as dev dependencies

npm i -D @types/node

Then, you can import the assert module :-)

import * as assert from 'assert';
sceee
  • 1,045
  • 12
  • 27
Yas
  • 3,443
  • 1
  • 29
  • 22
0

I had the same problem, and switched to power-assert which seems to work fine with:

import assert from 'power-assert'
James McLaughlin
  • 18,363
  • 2
  • 45
  • 56