TL;DR: setting compilerOptions.module
to "esnext"
will prevent ts-node
from converting import
statements which causes node to crash with a SyntaxError
.
“Verbose” Description
Given that
ts-node
automatically loads thetsconfig.json
unless it’s explicitly told not to (link)- and the
tsconfig.json
file isn’t necessarily configured to produce node-compatible code,
what can happen is that the gulpfile.ts
file is transpiled into code that isn’t executable by node.
This kind of problem can occur whenever the build target supports features that the local node version doesn’t and one of those features are used inside the gulpfile.ts
(which in my case was the import
statements).
Possible Solutions
My solution to this problem would be to prevent ts-node
to load the tsconfig.json
by setting the environment variable TS_NODE_SKIP_PROJECT
to true
, this would make it fall back to the defaults options which are to produce ES3+CommonJS code.
As far as
ts-node
is concerned the build target and the local node interpreter are the same, so it’s sane for them to loadtsconfig.json
by default.This assumption is untrue in Gulp’s case where the gulpfile is meant to be run locally by node but the actual project might target another version of node or even a different runtime like deno.