43
u/americk0 Sep 12 '20
All this whining about how to include other files. Just use one file to rule them all and it'll take so long to find the code you wanted to edit that you won't have time to complain
67
u/mean_pretense Sep 12 '20
Laughs in Typescript
32
u/zenthial Sep 12 '20
This is one of the little things about typescript you take for granted until you use node again
26
8
u/NoInkling Sep 13 '20 edited Sep 13 '20
Typescript (and by extension VS Code) doesn't play so nice with the new file extensions though, see:
This matters if you're using any sort of mixture of module types (and actually want to make use of the native ESM support).
4
u/LetReasonRing Sep 13 '20
I really need to learn me some typescript. I know why I should learn it. I know in the long run it will make my life easier. I have so much to do, picking up a new language is daunting.
11
u/mean_pretense Sep 13 '20
Don't think of Typescript as a brand new language. It is at its core, JavaScript combined with some cool features.
Thinking this way may make it feel less daunting.
4
u/netwrks Sep 13 '20
This. Learn javascript, and adopt typescript when you are put in a situation that requires you to learn it.
4
Sep 13 '20
Like when you try to add two 'numbers' together, but they're actually strings, but you don't realise this, so you are confused why 2 + 2 results in 22 and not 4. That's what led me to using TypeScript. If shit like that can happen without you being informed about it with a handy hint saying "hey, this is a string and not a number" then you're writing code in a world of chaos
0
u/netwrks Sep 13 '20
Or, you just learn from your mistake and remember ‘I need to convert all string numbers into Ints prior to doing anything with them’, or even better ‘there shouldn’t be a case where I’m ever put in a situation where I have to add a string and int together, let me figure out what’s going on and fix it’
I understand why people want to be guaranteed type safety, but for JS that’s not a reality. Even with TS, which does nothing for you at run time. So if you’re passing around questionable data, you’re still going to run into the issue. The only difference is TS allows you to make that mistake instead of understanding what went wrong
4
Sep 13 '20
Right, but the point of TS is to forgo the time and effort when debugging something that comes down to a wrong type being used. A mix-up between strings and numbers is a basic example but it's nice to be prodded by the IDE or TS compiler about something instead of not being prodded at all. A more common example would be in objects where a property is missing or is the wrong expected type. It's just a huge help with no downsides except the time it takes to learn the approach in manually typing values and working with strong typing.
The only difference is TS allows you to make that mistake instead of understanding what went wrong
This is the other way round, you mean JS right?
0
u/netwrks Sep 13 '20 edited Sep 13 '20
And by foregoing time and effort you make sacrifices and then other people make sacrifices and eventually the quality of what you’re build ends up suffering.
With JS, you get the same errors regardless of environment. By ‘allows’ im saying that TS excuses questionable code as long as youre mindful of types. I’ve come across quite a few cases like this
If you were rich, you could either cook or hire someone to cook. There are pros and cons to both, but only one of those options allows you to learn to cook on your own, which could result in high quality meals with less overhead/money.
2
u/nomadProgrammer Sep 13 '20
Nah bruh. TS is a subset of new features to JS. Trust me when I tell you it shouldn't take longer than 2 days to feel confident in TS. If you know JS.
Some more time to get confident with it
6
5
u/dev-ex__ph Sep 12 '20
Sometimes I made mistakes to type them interchangeably when working both React and Node. Haha!
18
u/JoeCamRoberon Sep 12 '20
Node >= v13
It's very simple in Node 13 and above. You need to either:
Save the file with .mjs extension, or Add { "type": "module" } in the nearest package.json. You only need to do one of the above to be able to use ES modules.
Node <= v12
If you are using Node version 8-12, Save the file with ES6 modules with .mjs extension and run it like:
node --experimental-modules my-app.mjs
14
u/_GCastilho_ Sep 12 '20
According to the docs, node 12.x already supports
"type": "module"in package.json-5
2
u/KilianKilmister Sep 13 '20
I mean nodejs has had flawless support of esm for quite a while. The issue is the tools that haven't adabted yet.
Been using esm almost exclusively for about 2 years now and it's such a pain having to use CommonJS in comparison
2
4
u/IngoVals Sep 13 '20
Do you believe that Deno will be the go to in the future instead of Node?
7
u/MatthewMob Sep 13 '20
No. Node has been around for years and is very mature at this point and is currently being used used by dozens of huge companies.
Deno is brand new and barely has any traction. It'll be a while before it is viable at all in a production environment.
4
u/IngoVals Sep 13 '20
Yeah, I'm afraid that could be true. I reckon Deno would be an improvement, at least in regards to security. But the infrastructure surrounding Node is so vast that it is hard to replace.
4
u/magizhchi Sep 13 '20
But the infrastructure surrounding Node is so vast that it is hard to replace
Sounds a lot like PHP
1
u/mypirateapp Sep 13 '20
as much as i hate to say this i really hate babel, it has given me more trouble to setup and maintain in any project than any other library
1
1
1
u/khoa_hd96 Sep 12 '20
We can use babel for this, but it's really confusing for nodejs newbie about these commonjs or es module.
0
Sep 12 '20
[deleted]
37
u/DepravedPrecedence Sep 12 '20
I believe you can specify module type in package.json to treat .js files as ES modules by default. Still not a great thing about that new extension.
26
u/Comesa Sep 12 '20
yes, you can simply put
"type": "module"in your package.json and it will use es modules by default.9
u/crrime Sep 12 '20
Is this supported in the current LTS version of node? I couldn't seem to get it to work in my last project
7
u/Comesa Sep 12 '20
12.x LTS does have this feature according to the docs
4
u/crrime Sep 12 '20
Well I'll be damned, looks like I was on an older 12.x version. Thanks! Will definitely upgrade and change my imports
3
2
u/8lbIceBag Sep 12 '20
I ran into issues (can't remember what, maybe webpack related) when I tried that. Also because you can't do top level await in node v12. Eventually just decided to avoid import all together.
5
1
Sep 14 '20
[removed] — view removed comment
1
Sep 14 '20
I don't dislike require. I don't like the fact that import and require are completely incompatible with each other to the degree that it's a syntactically error to the other. So lodash-es has to be a separate package from lodash.
53
u/jkoudys Sep 12 '20
Just migrate as much of your logic as you can client-side. Problem solved!