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:
$ ts-node index.ts
(node:32890) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
-> import runBatch from "src/runBatch";
$ ts-node index.ts
(node:32890) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
-> import runBatch from "src/runBatch";
Relivant lines in tsconfig.ts:
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
40 Replies
Brendonovich
Brendonovich2y ago
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 files
Samathingamajig
this is why i've given up on trying to do ts outside of a framework
Brendonovich
Brendonovich2y ago
weakling
Samathingamajig
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
fotoflo
fotoflo2y ago
@Brendonovich i tried adding module but i get a whole slew of newer errors @Samathingamajig this is a backend crawling application
Brendonovich
Brendonovich2y ago
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
fotoflo
fotoflo2y ago
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
Brendonovich
Brendonovich2y ago
wait what did u change to ES2020?
fotoflo
fotoflo2y ago
target, lib and module
Brendonovich
Brendonovich2y ago
isn't that what u already have? oh wait nvm i think i get what u mean
fotoflo
fotoflo2y ago
it was ES6 before or something it was default before the whoel project was javascript and i migrated it
Brendonovich
Brendonovich2y ago
have u tried out vitest?
fotoflo
fotoflo2y ago
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.
fotoflo
fotoflo2y ago
fotoflo
fotoflo2y ago
i thought this should work googling vitest
Brendonovich
Brendonovich2y ago
nah change it to CommonJS that'll transpile import to require node is a lot more friendly with require
fotoflo
fotoflo2y ago
fotoflo
fotoflo2y ago
with commonjs
Samathingamajig
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 typescript
Brendonovich
Brendonovich2y ago
it should be module: CommonJS keep the rest as is
fotoflo
fotoflo2y ago
hmm it looks like its working until wtf oh oh its working
Brendonovich
Brendonovich2y ago
noice
fotoflo
fotoflo2y ago
i think something missing in the .env file gonna work thank you!!
Brendonovich
Brendonovich2y ago
never try to use esmodules properly with node haha, just transpile to commonjs
fotoflo
fotoflo2y ago
i didnt get it before "module": "CommonJS", "target": "ES2020", means transpile from module to target what is lib?
Brendonovich
Brendonovich2y ago
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 types
fotoflo
fotoflo2y ago
damn you should write the documentation
Brendonovich
Brendonovich2y ago
lmao
fotoflo
fotoflo2y ago
haha docs are so impossible thank you @Brendonovich
Brendonovich
Brendonovich2y ago
happy to help!
fotoflo
fotoflo2y ago
so now i have a mix of require and import looks like i can move everythign to import require might cause bugs
Brendonovich
Brendonovich2y ago
nah require will be stable dw all the imports are just getting turned into requires anyway
fotoflo
fotoflo2y ago
fotoflo
fotoflo2y ago
appears to be working!
Brendonovich
Brendonovich2y ago
W
fotoflo
fotoflo2y ago
sick thank you !!! this damn issue caused 2 motorcycle rides
Brendonovich
Brendonovich2y ago
that doesn't sound too bad haha
fotoflo
fotoflo2y ago
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
/** @type {import('ts-jest').JestConfigWithTsJest} */

const { pathsToModuleNameMapper } = require("ts-jest");
const { compilerOptions } = require("./tsconfig.json");

module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: "<rootDir>/",
}),
};
/** @type {import('ts-jest').JestConfigWithTsJest} */

const { pathsToModuleNameMapper } = require("ts-jest");
const { compilerOptions } = require("./tsconfig.json");

module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: "<rootDir>/",
}),
};
Brendonovich
Brendonovich2y ago
hmm, maybe u gotta keep them as requires, idk
fotoflo
fotoflo2y ago
... i used the import syntax in some JS files... moved them to ts file and voilà!