Ts-node: I'm trying to use ts-node with standard-things/esm. I cannot get it to work.

Created on 31 Oct 2018  路  7Comments  路  Source: TypeStrong/ts-node

Hi,
So I decided to switch to TypeScript today. In my revious node setup, I was using https://github.com/standard-things/esm for ESM import/export, as Node doesn't support it yet. When trying to run my start script with ts-node -r esm mrFox.ts, I get an error, like so:

import { token } from "./config.json";
       ^

SyntaxError: Unexpected token {
    at new Script (vm.js:79:7)
    at Object.m._compile (/usr/lib/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/usr/lib/node_modules/ts-node/src/index.ts:442:12)

This seems to be a result of esm not loading? not sure.
Please help, I would like to be able to use esm with node and ts-node.

invalid

Most helpful comment

Given that you probably have to do something like node -r esm -r ts-node/register.

All 7 comments

In the README of that module it says you need to load it before ts-node.

It does? Where?

image

Given that you probably have to do something like node -r esm -r ts-node/register.

@blakeembrey your solution worked, thanks!

Given that you probably have to do something like node -r esm -r ts-node/register.

For some reason, when i run this command more than once, i receive different results. First time is fine, but subsequent times require me to go to another branch and come back which seems very odd. Any suggestions?

Given that you probably have to do something like node -r esm -r ts-node/register.

For some reason, when i run this command more than once, i receive different results. First time is fine, but subsequent times require me to go to another branch and come back which seems very odd. Any suggestions?

I just needed to disable the cache as outlined in the options docs https://github.com/standard-things/esm#options

ESM_DISABLE_CACHE=true TS_NODE_PROJECT=scripts/tsconfig.json node -r esm -r ts-node/register scripts/index.ts
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cevek picture cevek  路  4Comments

nehalist picture nehalist  路  3Comments

mattdell picture mattdell  路  4Comments

aj-r picture aj-r  路  3Comments

Borewit picture Borewit  路  3Comments