joelanman,
@joelanman@hachyderm.io avatar

In Node you can easily load json like this:

const myData = require('data.json')  

is there an equivalent in the new import syntax?

joelanman,
@joelanman@hachyderm.io avatar

and if not, is there anything wrong with using require here even if you're using import for everything else?

siblingpastry,
@siblingpastry@mastodon.world avatar

@joelanman I don't know about Node, but ES6 has import() which returns a Promise. Is that what you're looking for? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import

joelanman,
@joelanman@hachyderm.io avatar

so you can't use require if package.json has type: module. This seems to work but feels like a step backwards

import myData from './data.json' assert { type: 'json' }  

https://stackoverflow.com/a/76030578

bcdavid,
@bcdavid@hachyderm.io avatar

@joelanman Import type assertions are new, but that is going to be the "proper" way to do it with native ESM going forward. Otherwise the only option is to import filesystem.read and directly read the file. I don't consider it a step backwards; having oddities in Common JS such as being able to import non-JS files has always caused challenges with JS tooling. With import types, it's much easier for tooling to know exactly what it is the import is doing without having to guess.

joelanman,
@joelanman@hachyderm.io avatar

@bcdavid genuine question, why isnt the file extension enough?

bcdavid,
@bcdavid@hachyderm.io avatar

@joelanman There's the usual reason: file extensions are only names and can't guarantee anything about what is in the file. Then there's JavaScript reasons, that take a whole lot longer to explain. So I'll throw it into a thread. 1/7

bcdavid,
@bcdavid@hachyderm.io avatar

Node's implementation of Common JS is similar to other module systems. It expects that what you are importing is a text file that is another module, and then uses a Node-specific resolution algorithm to find the module. CJS imports are also done synchronously. 2/7

bcdavid,
@bcdavid@hachyderm.io avatar

This means that Node was able to add unique syntactical sugar to imports: bare imports of JS files without extensions; bare imports using folder names as long as there's an index.js in the folder; synchronous in-line require; and specific support for files with .json extensions, where it will do the work of of parsing the file as JSON for you. 3/7

bcdavid,
@bcdavid@hachyderm.io avatar

ES Modules are designed for the Web and use URIs as module specifiers. So the file extension has to be in the specifier because there isn't a really resolution algorithm outside of the URI indicating exactly where the file is. It can even be completely external using a web URL. So ES module imports are always asynchronous and can never assume the file type by the extension. 4/7

bcdavid,
@bcdavid@hachyderm.io avatar

Like other module systems, ESM still requires the imported file be a text file that is another ES module. The problem is that in modern web-dev, people have many use cases for importing non-JS files; JSON and CSS being the prime examples. 5/7

bcdavid,
@bcdavid@hachyderm.io avatar

In order to provide a way for a URI-based module system to import files that aren't modules in that programming language, you need to provide both the URI and an assertion of what kind of content is in that file. Then the run-time can choose to help you out in a standardized way, such as automatically parsing JSON files. 6/7

bcdavid,
@bcdavid@hachyderm.io avatar

Hopefully that makes sense. This TC39 proposal goes into detail on all of this: https://github.com/tc39/proposal-import-attributes

Also, I've never done a thread in Mastodon, so hopefully the format works. 7/7

joelanman,
@joelanman@hachyderm.io avatar

@bcdavid wow thanks for the in depth explainer!

siblingpastry,
@siblingpastry@mastodon.world avatar

@joelanman Both require and import can be used as statements or dynamic functions. The statement syntax requires module import, but the function supports anything.

(Still talking ES6, don't know about Node specifically)

joelanman,
@joelanman@hachyderm.io avatar

@siblingpastry I'm talking about Node and it doesnt work, just tried it, but the solution I posted does (though it says it is experimental)

siblingpastry,
@siblingpastry@mastodon.world avatar

@joelanman So you want to import a module, but require() doesn't support that, and require isn't available?

joelanman,
@joelanman@hachyderm.io avatar

@siblingpastry no I'm talking about JSON data

siblingpastry,
@siblingpastry@mastodon.world avatar

@joelanman Oh okay, so nothing I said was helpful 😷

Ah well lol. I could rattle off a whole bunch of different JSON import ideas, but nothing you won't have already thought of.

joelanman,
@joelanman@hachyderm.io avatar

found another gotcha, you could use require to load json any time, import can only be used to load json in the root scope (not in functions for example)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming
  • kavyap
  • thenastyranch
  • Durango
  • DreamBathrooms
  • ngwrru68w68
  • magazineikmin
  • cisconetworking
  • Youngstown
  • mdbf
  • slotface
  • osvaldo12
  • GTA5RPClips
  • rosin
  • InstantRegret
  • provamag3
  • everett
  • cubers
  • vwfavf
  • normalnudes
  • tacticalgear
  • tester
  • ethstaker
  • khanakhh
  • modclub
  • Leos
  • anitta
  • megavids
  • JUstTest
  • All magazines