HerringtonDarkholme / typescript-repl

An upgraded TypeScript REPL
210 stars 16 forks source link

exports is not defined #37

Open perplexes opened 5 years ago

perplexes commented 5 years ago
// export_default.ts
export async function process(filename: string): Promise<any> {
  return {};
}

export default process;
$ npm list tsun
└── tsun@0.5.0
$ node_modules/.bin/tsun
TSUN : TypeScript Upgraded Node
type in TypeScript expression to evaluate
type :help for commands in repl

> import p from 'export_default';
ReferenceError: exports is not defined
    at evalmachine.<anonymous>:3:23
    at ContextifyScript.Script.runInContext (vm.js:35:29)
    at Object.runInContext (vm.js:89:6)
    at startEvaluate (node_modules/tsun/bin/src/repl.js:147:25)
    at replLoop (node_modules/tsun/bin/src/repl.js:173:9)
    at node_modules/tsun/bin/src/repl.js:311:9
    at Interface._onLine (readline.js:231:5)
    at Interface._line (readline.js:585:8)
    at Interface._ttyWrite (readline.js:862:14)
    at ReadStream.onkeypress (readline.js:120:10)
zaoqi-unsafe commented 5 years ago

:load gives the same error

thesoftwarephilosopher commented 5 years ago

Having the same error here:

$ tsun
TSUN : TypeScript Upgraded Node
type in TypeScript expression to evaluate
type :help for commands in repl

> import fs from 'fs'
evalmachine.<anonymous>:3
Object.defineProperty(exports, "__esModule", { value: true });
                      ^

ReferenceError: exports is not defined
    at evalmachine.<anonymous>:3:23
    at Script.runInContext (vm.js:135:20)
    at Object.runInContext (vm.js:298:6)
    at startEvaluate (/usr/local/lib/node_modules/tsun/bin/src/repl.js:147:25)
    at replLoop (/usr/local/lib/node_modules/tsun/bin/src/repl.js:173:9)
    at /usr/local/lib/node_modules/tsun/bin/src/repl.js:311:9
    at Interface._onLine (readline.js:305:5)
    at Interface._line (readline.js:680:8)
    at Interface._ttyWrite (readline.js:960:14)
    at ReadStream.onkeypress (readline.js:183:10)
> 
benhili commented 4 years ago

this makes any module with an import statement unusable in tsun for me

RossComputerGuy commented 3 years ago

I'm having the same issue

jq170727 commented 1 year ago

Seeing same problem. For reference here's what I did

% pnpm i -D typescript @types/node tsun
% tsun
  node:internal/fs/utils:346
      throw err;
      ^
Error: ENOENT: no such file or directory, open 'xxx/tsun1/node_modules/.pnpm/tsun @0.5.1_typescript@4.9.5/node_modules/tsun/node_modules/@types/node/index.d.ts' at Object.openSync (node:fs:585:3) at Object.readFileSync (node:fs:453:35) at xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/bin/src/service.js:97:25 at Object. (xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/bin/src/service.js:107:3) at Module._compile (node:internal/modules/cjs/loader:1246:14) at Module._extensions..js (node:internal/modules/cjs/loader:1300:10) at Module.load (node:internal/modules/cjs/loader:1103:32) at Module._load (node:internal/modules/cjs/loader:942:12) at Module.require (node:internal/modules/cjs/loader:1127:19) at require (node:internal/modules/helpers:112:18) { errno: -2, syscall: 'open', code: 'ENOENT', path: 'xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/node_modules/@types/node/index.d.ts' } Node.js v19.6.0

Looks like this is https://github.com/HerringtonDarkholme/typescript-repl/issues/33

Apply that workaround....

% pushd xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/node_modules
  xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/node_modules xxx/tsun1
% ln -s ../../@types .
% popd

Here are the files I'm trying to load.

% cat foo.ts
  import bar from './bar'
  export function foo():string { return bar() }

% cat bar.ts
  export default function bar():string { return "bar says foo!" }

Let's try again

% tsun
  TSUN : TypeScript Upgraded Node
  type in TypeScript expression to evaluate
  type :help for commands in repl

  > :help

  tsun repl commands
  :type symbol       print the type of an identifier
  :doc  symbol       print the documentation for an identifier
  :clear             clear all the code
  :print             print code input so far
  :help              print this manual
  :paste             enter paste mode
  :load filename     source typescript file in current context
  > :load foo.ts
evalmachine.:2 Object.defineProperty(exports, "__esModule", { value: true }); ^ ReferenceError: exports is not defined at evalmachine.:2:23 at Script.runInContext (node:vm:140:12) at Object.runInContext (node:vm:290:6) at startEvaluate (xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/bin/src/repl.js:148:25) at loadFile (xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/bin/src/repl.js:213:9) at xxx/tsun1/node_modules/.pnpm/tsun@0.5.1_typescript@4.9.5/node_modules/tsun/bin/src/repl.js:304:13 at [_onLine] [as _onLine] (node:internal/readline/interface:421:7) at [_line] [as _line] (node:internal/readline/interface:894:18) at Interface._ttyWriteDumb (node:readline:500:14) at ReadStream.onkeypress (node:internal/readline/interface:271:20)

zx is a little better

% pnpm i -D zx
% zx --repl
  ❯ .help
  .break    Sometimes you get stuck, this gets you out
  .clear    Alias for .break
  .editor   Enter editor mode
  .exit     Exit the REPL
  .help     Print this help message
  .load     Load JS from a file into the REPL session
  .save     Save all evaluated commands in this REPL session to a file

  Press Ctrl+C to abort current expression, Ctrl+D to exit the REPL
  ❯ .load foo.ts
  import bar from './bar'
  export function foo():string { return bar() }

  ❯ foo()
  import bar from './bar'
  ^^^^^^

  Uncaught:
  SyntaxError: Cannot use import statement inside the Node.js REPL, alternatively use dynamic import
  ❯ 

ts-node seems to handle this

% pnpm i -D ts-node
% ts-node
  > .load foo.ts
  import bar from './bar'
  export function foo():string { return bar() }

  > foo()
  'bar says foo!'
  > 
%