Ts-node: Support flags like allowSyntheticDefaultImports?

Created on 22 Mar 2016  Â·  15Comments  Â·  Source: TypeStrong/ts-node

I have:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "module": "commonjs",
    ...
  }
}

and try to run a file using that feature and it fails.

This is for my team transitioning from JS to TS so I want to keep it as simple for them as possible (I control the environment so the behavior of import dr from 'domready'; is consistent.

Does ts-node honor this flag?

(I also notice module: system doesn't work either)

external wontfix

All 15 comments

If it's not working, it's because it does not work from the TypeScript compiler. This is only a thin layer on top of the compiler. Also, no other module system other than CommonJS will currently work - Node.js does not natively support SystemJS so it doesn't make sense.

If it's not working, it's because it does not work from the TypeScript compiler.

That's a bit strange. I make sure the global and local TypeScript are all 1.8.9.

Is allowSyntheticDefaultImports supported by CommonJS though?

Yes, that's what it is for. module: system by default has allowSysntheticDefaultImports: true because that's SystemJS's behavior.

allowSysntheticDefaultImports has no effect on others, i.e. amd, global.

I don't see this working then. Node doesn't do SystemJS, it does CommonJS.

allowSysntheticDefaultImports has no effect on others, i.e. amd, global.

By that I mean allowSysntheticDefaultImports only applies to system and commonjs. system defaults it to true, and commonjs default it to false.

Since ts-node runs tsc and output in commonjs, it "should" work in Node. Although this comment concerns me:
https://github.com/Microsoft/TypeScript/wiki/Compiler-Options

Allow default imports from modules with no default export. This does not affect code emit, just typechecking.

Yeah, that seems like the issue here. I assume the error you run into is a runtime JavaScript error then and not tsc with this enabled?

I've also noticed the same behaviour, even though I've set allowSyntheticDefaultImports to true. When compiling the same file with tsc -p tsconfig.json, I don't get any errors.

But ts-node fails with:

Module ''object-assign'' resolves to a non-module entity and cannot be imported using this construct. (2497)

When running ts-node in transpileModule mode, everything compiles and executes correctly.

Please re-read the issue. TypeScript Node does not resolve using SystemJS.

What do you mean? I did set module to commonjs.

Then it should be erroring when you use tsc. You can't use allowSysntheticDefaultImports with CommonJS imports that should be import x = require('') to make it magically work for you, it doesn't change the emit and is only built as a polyfill for runtimes like SystemJS that do magic instead. I would recommend you stick to standard ways of importing instead.

I was having this problem today and thanks to @blakeembrey post I figured out what I was misunderstanding.

a few erroneous assumptions without looking at the documentation (doh!):

  • allowSyntheticDefaultImports does not make a difference loading commonjs modules. (e.g. node modules)
  • It also does not make a difference when loading your own typescript files with export default defined.

It only offers type checking support as described from the docs:
"Allow default imports from modules with no default export. This does not affect code emit, just typechecking."

after switching to module commonjs in my tsconfig.json file and using import x = require('someCommonJsOrNodeModule'), it worked fine. And I appear to still have auto completion in my ide (webstorm)

The same issue:

➜ ts-node test.ts 

/usr/local/lib/node_modules/ts-node/src/index.ts:206
      throw new TSError(formatDiagnostics(configDiagnostics, cwd, ts, 0))
            ^
TSError: ⨯ Unable to compile TypeScript
Unknown compiler option 'awesomeTypescriptLoaderOptions'. (5023)
    at load (/usr/local/lib/node_modules/ts-node/src/index.ts:206:13)
    at service (/usr/local/lib/node_modules/ts-node/src/index.ts:370:32)
    at Object.register (/usr/local/lib/node_modules/ts-node/src/index.ts:379:5)
    at Object.<anonymous> (/usr/local/lib/node_modules/ts-node/src/_bin.ts:148:17)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)

I could not compile with the following options: awesomeTypescriptLoaderOptions and allowSyntheticDefaultImports

@monolithed That doesn't look remotely like the same issue, if you read this issue. Please open a new thread instead of polluting unrelated issues. For one, that's a TypeScript error because of exactly what is says - awesomeTypescriptLoaderOptions is an unknown compiler option.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattdell picture mattdell  Â·  4Comments

watzon picture watzon  Â·  3Comments

cibergarri picture cibergarri  Â·  3Comments

max-block picture max-block  Â·  4Comments

Borewit picture Borewit  Â·  3Comments