TLDR: When "module" is anything aside from "commonjs" you need to explicitly specifiy a "moduleResolution" value of "node" for this to work. Setting "commonjs"for"module" does so implicitly.
As this is by no means obvious, I strongly encourage explicitly specifying "moduleResolution" whenever specifying "module".
I strongly recommend that you always specify the output module format explicitly.
Additionally, despite the names of certain options, "target" and "module" are independent and orthogonal. They mean very different things and must not be confused.
{
"compilerOptions": {
"outDir": "./dist/",
"module": "esnext",
"moduleResolution": "node",
"target": "esnext", // this isn't relevant
"allowJs": true,
"sourceMap": true
}
}
"commonjs" is an output module format. ESNext dynamic import(...) statements are a transpiled into the output module format just like other module syntax such as ES2015 import and export statements.
When you specify --module esnext, you are telling TypeScript not to transpile any module syntax at all. That's the point of --module it specifies the output module format, not the source module format.
Hey, I'm working on a small side project and learning node and just can't wrap my head around what's happening here. Please forgive if this has already been answered for someone else. I searched around and the answers don't quite do it for me.
My situation is this. I've installed create-react-app, graphql, and apollo client/server. I'm working on getting the server set up and through that have added a few lines to the server file that use an import statement.
The guide that I was following to set up the server however uses Common JS. This confuses me.
I don't understand why one is used over the either. I'm getting an error now when I init the server file that says "Cannot use import statement outside of a module". I understand that this means that the import statement isn't valid in my current file but I don't know why it's invalid in this file but perfectly fine in my react typescript files.
Can someone please explain to me why/when I should be using import vs. require, and why it only works in certain files and folders?
Thank you in advance!
Commonjs module system instead of ESNext
node modules - Typescript: esnext compiler option destroys es6 import from external lib - Stack Overflow
"esnext" breaks import/export syntax from commonjs? (tsconfig.json)
Docs: Difference between esnext, es6, es2015 module targets
Videos
I'm (simply) trying to use top-level exports... I did have my module set to commonjs and target to es6 but you need to set module to esnext to get top-levl await.
The issue is that setting modules to esnext has broken import/export.
I can't find any simple breakdown of the differnce between esnext modules and commonjs. Can anyone tell me why import/export would break like this?
Or a simple tsconfig.json example that will work for top-level await
All I want is a simple config.ts file where I can export env variables, but I need to get the variables from aws secret manager
New to Node, and was fortunate to have the choice between module types. I preferred ESM syntax, did a bit of research into the differences between the two and it seemed the posts I were reading were suggesting ESM over CJS.
So that's cool but I've been running into weird issues where some dependencies need CommonJS, so I can't set "type": "module" in package.json
There are probably third party libraries that will aid me in these pains, but I'm up to my ears in third party libraries and thinking that I have to add yet another one for a fairly simple app is a bit cringey.
Unfortunately my desire to use ESM modules have resulted in quite a few hours of frustration as a newbie.
Sidenote: I was excited to learn Typescript because it seemed cool, but then I read a few articles that say turning a dynamically typed language to a static language is stupid. Except they went into much more depth into why it is stupid, and not gonna lie, it sounded convincing. But again, I'm just a newb. The JS universe is kinda quirky, eh.