Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError with tsx file ending. #1178

Closed
NaridaL opened this issue Dec 16, 2020 · 3 comments · Fixed by #1179
Closed

SyntaxError with tsx file ending. #1178

NaridaL opened this issue Dec 16, 2020 · 3 comments · Fixed by #1179

Comments

@NaridaL
Copy link
Contributor

NaridaL commented Dec 16, 2020

Expected Behavior

According to the readme, ts-node registers itself for file endings .tsx?, i.e. .ts and .tsx. Therefore the behavior should be the same for both file endings.

Actual Behavior

SyntaxError: Cannot use import statement outside a module with.tsx file

Steps to reproduce the problem

# create a test.ts file
$ echo "import * as path from 'path'" >> test.ts
$ echo "console.log(Object.keys(path))" >> test.ts

# works as expected
$ ts-node test.ts
[
  'resolve',    'normalize',
  'isAbsolute', 'join',
  'relative',   'toNamespacedPath',
  'dirname',    'basename',
  'extname',    'format',
  'parse',      'sep',
  'delimiter',  'win32',
  'posix',      '_makeLong'
]

# copy to test.tsx
$ cp test.ts test.tsx

# try running it with ts-node
$ ts-node test.tsx
(node:19628) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\aval\tsdev\chroma.ts\ts-node-error\test.tsx:1
import * as path from 'path'
^^^^^^

SyntaxError: Cannot use import statement outside a module
...

# -O does nothing
$ ts-node -O "{\"module\":\"commonjs\"}" test.tsx
(node:1784) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\aval\tsdev\chroma.ts\ts-node-error\test.tsx:1
import * as path from 'path'
^^^^^^

SyntaxError: Cannot use import statement outside a module
...

Minimal reproduction

see above

Specifications

  • ts-node v9.1.1

  • node v14.15.2

  • compiler v4.1.3

  • tsconfig.json, if you're using one: none, see above

  • Operating system and version:

image

  • If Windows, are you using WSL or WSL2?: no
@cspotcode
Copy link
Collaborator

It'll only register if you have "jsx" enabled in your tsconfig.json.

@cspotcode
Copy link
Collaborator

Here's our registration implementation:
https://github.com/TypeStrong/ts-node/blob/master/src/index.ts#L393-L403

@NaridaL
Copy link
Contributor Author

NaridaL commented Dec 16, 2020

@cspotcode Figured it out too after I posted 😄. Having this work out of the box probably doesn't make sense, as TS itself requires the jsx optionto be set.

I've improved the docs #1179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants