question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Unexpected token 'export'` when exporting `wrapValidatorAsTypeguard` with a ts-node build

See original GitHub issue

Hello, thanks for writing this fantastic library, it has been useful so far!

I’m getting the following error when building with ts-node:

$ cross-env NODE_ENV=development nodemon --exec ts-node -r dotenv-flow/config -r tsconfig-paths/register src/index.ts | pino-pretty -c
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node -r dotenv-flow/config -r tsconfig-paths/register src/index.ts`
/home/lcsmuller/PF/quickstart-nodejs-rest/node_modules/json-schema-to-ts/lib/index.js:1
export { wrapCompilerAsTypeGuard, wrapValidatorAsTypeGuard, } from "json-schema-to-ts/lib/type-guards";
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .js] (/home/lcsmuller/PF/quickstart-nodejs-rest/node_modules/ts-node/src/index.ts:1361:43)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/lcsmuller/PF/quickstart-nodejs-rest/src/common/schemas/utils/helpers.ts:3:1)
[nodemon] app crashed - waiting for file changes before starting...

This error only seems to trigger when I attempt to export a method that comes from a .js file (wrapValidatorAsTypeguard or wrapCompilerAsTypeguard)… It appears that ts-node cannot support export and import syntax by default, I think an easy fix for this would be targeting es5 instead (unless there is a rationale for not doing so). If you are busy, I can give it a try to write the PR.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasAribartcommented, Dec 1, 2022

Nice 👍 ! 2.6.2 is out so you can switch to an official version ! Cheers !

1reaction
ThomasAribartcommented, Nov 29, 2022

@lcsmuller Normally you shouldn’t need to worry about that, it’s done under the hood by node (I think ts-node will use commonjs imports though).

See https://webreflection.medium.com/cjs-vs-esm-5f8b90a4511a

TL:DR, cjs is the “old” way of importing JS, still needed for backward compatibility, esm is the “new” way. The package.json exposes two keys, main and module, so both code can be used.

Okay I’ll investigate some more 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Unexpected token 'export' in Nodejs project with ...
In project B, I have several classes, 2 of which are exported, plus some other definitions. In project B's index.ts: export * from...
Read more >
Can't get simple script to run due to SyntaxError: Unexpected ...
Tell the author that this fails on your system: npm ERR! ts-node bin/script.ts npm ... I started , receiving "Unexpected token 'Export'".
Read more >
Unexpected token 'export' : r/typescript - Reddit
I'm started using typescript again with express. I'm just playing around and build the API at the moment and I can't import a...
Read more >
TypeScript errors and how to fix them
If you want to export a constant from a definition file ( d.ts ), then you ... If you need a more precise...
Read more >
Typescript : SyntaxError: Unexpected token 'export'-babel.js
You can fix this by changing to CommonJS, or following the steps outlined in the NodeJS ESM page linked. As for why this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found