Ts-node: ReferenceError: exports is not defined (OSX)

Created on 15 Apr 2017  Â·  5Comments  Â·  Source: TypeStrong/ts-node

I have an error message if I use lib with attached typings (e.g. moment, lodash):

import * as moment from 'moment'
ReferenceError: exports is not defined
    at [eval].ts:2:23
    at ContextifyScript.Script.runInContext (vm.js:35:29)
    at ContextifyScript.Script.runInNewContext (vm.js:41:15)
    at _eval (/Users/shamsheev/.nvm/versions/node/v6.4.0/lib/node_modules/ts-node/dist/_bin.js:181:29)
    at REPLServer.replEval (/Users/shamsheev/.nvm/versions/node/v6.4.0/lib/node_modules/ts-node/dist/_bin.js:219:18)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:503:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:188:7)

But I can use library after error without any problems.
ts-node v3.0.2
node v6.4.0
tsc Version 2.2.2

bug external research

Most helpful comment

Awesome, thanks. I managed to replicate it. Unfortunately, it's the result of a change in TypeScript. When using modules now, TypeScript is auto-magically adding:

Object.defineProperty(exports, "__esModule", { value: true })

I'm not sure of the best workaround because the REPL has no export. The easiest thing would be to deviate from node.js here and just provide a top-level exports = {} object inside the REPL. However, it might be more useful if TypeScript didn't emit this (except on exports) or if there was a flag to specify "no exports module" of the current file, except it allows imports.

/cc @mhegazy

All 5 comments

Can you share on more information on replicating it? What's your tsconfig.json file? I can't really help with the information provided.

@blakeembrey I don't use own tsconfig.json with REPL.

mkdir ts-node__test
cd ts-node__test
npm i moment
ts-node

then

> import * as moment from 'moment'
ReferenceError: exports is not defined
    at [eval].ts:2:23
    at ContextifyScript.Script.runInContext (vm.js:35:29)
    at ContextifyScript.Script.runInNewContext (vm.js:41:15)
    at _eval (/Users/shamsheev/.nvm/versions/node/v6.4.0/lib/node_modules/ts-node/dist/_bin.js:181:29)
    at REPLServer.replEval (/Users/shamsheev/.nvm/versions/node/v6.4.0/lib/node_modules/ts-node/dist/_bin.js:219:18)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:503:10)
    at emitOne (events.js:96:13)
    at REPLServer.emit (events.js:188:7)
> const date = moment()
undefined
> date.format('LLLL')
'Sunday, April 16, 2017 11:40 AM'
> date.format(1)
⨯ Unable to compile TypeScript
[eval].ts (1,13): Argument of type '1' is not assignable to parameter of type 'string'. (2345)

Awesome, thanks. I managed to replicate it. Unfortunately, it's the result of a change in TypeScript. When using modules now, TypeScript is auto-magically adding:

Object.defineProperty(exports, "__esModule", { value: true })

I'm not sure of the best workaround because the REPL has no export. The easiest thing would be to deviate from node.js here and just provide a top-level exports = {} object inside the REPL. However, it might be more useful if TypeScript didn't emit this (except on exports) or if there was a flag to specify "no exports module" of the current file, except it allows imports.

/cc @mhegazy

My decision enough ugly but it can be used as a temporal solution.
[ TSC ]

class ESMExportsError extends ReferenceError {
    get name() { return 'ReferenceError' } // instanceof problem with runInNewContext
    get tsError() { return true }
}

[ TSC ]

if (typeof exports === 'undefined') { throw new ESMExportsError('exports is not defined') }
Object.defineProperty(exports, "__esModule", { value: true })

Thus it can be possible to use runInNewContext with "correct" error handling.

Confirming that the exports = {} workaround solves things for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OliverJAsh picture OliverJAsh  Â·  3Comments

watzon picture watzon  Â·  3Comments

JoseLion picture JoseLion  Â·  3Comments

joshua-tj picture joshua-tj  Â·  3Comments

nehalist picture nehalist  Â·  3Comments