tsconfig and import statements
All my tests (of the modules) are using import statements...
However once i put an import statement in the index.ts i get this error:
Relivant lines in tsconfig.ts:
40 Replies
I think it's bc u have
module: ES2020
, I ran into something like this a while ago using a more modern moduleResolution
setting
I think module: ES2020
makes ts-node
preserve the import
statements, which u can only use in node if u fully buy into ES modules and add type: module
or use .mjs
filesthis is why i've given up on trying to do ts outside of a framework
weakling
if i want to do ts stuff outside of a framework nowadays, i just use deno or bun
node is unbearable to work with typescript
@Brendonovich i tried adding module but i get a whole slew of newer errors
@Samathingamajig this is a backend crawling application
yeeeeah
type: module
is never fun
i think you'll want to change your target
or module
such that it changes the import
to require
ive actually been having this error on and off for a while
i changed it to ES2020 and it went away for the tests
but then bang! try running the index file
... so i went for a motorcycle ride - sometimes that fixes things
wait what did u change to
ES2020
?target, lib and module
isn't that what u already have?
oh wait nvm i think i get what u mean
it was ES6 before or something
it was default before
the whoel project was javascript and i migrated it
have u tried out vitest?
TSConfig Reference - Docs on every TSConfig option
From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.
i thought this should work
googling vitest
nah change it to
CommonJS
that'll transpile import
to require
node is a lot more friendly with require
with commonjs
i think you're supposed to make typescript output commonjs and not use
"type": "module"
in package.json
, but you can still use esmodules in typescriptit should be
module: CommonJS
keep the rest as ishmm
it looks like its working until
wtf
oh oh its working
noice
i think something missing in the .env file
gonna work
thank you!!
never try to use esmodules properly with node haha, just transpile to commonjs
i didnt get it
before
"module": "CommonJS", "target": "ES2020",
means transpile from module to target
what is lib?
target
is the js version you put in
module
is the js version u get out
lib
just specifies what js version's native types should be included, as well as stuff like dom typesdamn you should write the documentation
lmao
haha
docs are so impossible
thank you @Brendonovich
happy to help!
so now i have a mix of require and import
looks like i can move everythign to import
require might cause bugs
nah require will be stable dw
all the imports are just getting turned into requires anyway
appears to be working!
W
sick
thank you !!!
this damn issue caused 2 motorcycle rides
that doesn't sound too bad haha
could be worse haha
.../test/scores.test.js:1
({"Object.<anonymous>":function(module,exports,require,dirname,filename,jest){import prisma from "src/prisma";
^^^^^^
SyntaxError: Cannot use import statement outside a module
oh no its back
this time in the tests
i changed some requires to imports
like most of them
this is my jest.config
hmm, maybe u gotta keep them as requires, idk
... i used the import syntax in some JS files... moved them to ts file and voilà!