Burt_Harris Burt_Harris - 1 month ago 20
TypeScript Question

What is the TypeScript 2.0 / ES2015 way to import assert from Node.js?

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.

Answer

This works for me:

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 ...

Comments