DEV Community

Discussion on: Import Local JSON In Node.js v8.5 Experimental Modules

Collapse
 
danieljsummers profile image
Daniel J. Summers

I just brought in Babel to let me use this in every file except the first one. I was a bit surprised when import appConfig from './appconfig.json' worked, but then I realized Babel was changing the import to require(), which works as per usual. I'd read the "new in 8.5.0" post, but being relatively inexperienced with JS, I'd missed that part (or, at least, the implications of that lone bullet point).

I appreciate the snippet; I now know what my next commit will include. :) Thanks for the write-up!

Collapse
 
geoff profile image
Geoff Davis

Glad I could help! I was migrating one of my Node projects over to this new mjs style, and noticed that this could be a problem if people relied on data stored in their package.json or some other JSON file.

Collapse
 
danieljsummers profile image
Daniel J. Summers

I'm guessing the new mjs thing is an all-or-nothing thing? I tried just adding the --experimental-modules flag on my existing app, and it didn't like it. However, I've finally had success getting my Vue front-end to send the data to the API, it process it, and return something that makes sense. (woo hoo!)

I'll defer playing with modules for a bit, though I do have an .mjs file there (you can old-style require them, too, if you add the extension) to remind me.

Thread Thread
 
geoff profile image
Geoff Davis

I'm guessing the new mjs thing is an all-or-nothing thing?

I believe that is the case. To be honest I do not know much about how Node works behind-the-scenes, but it appears that switching between --experimental-modules and CommonJS modules would be like using straight ES201* code or sending it through Babel.

... I do have an .mjs file there (you can old-style require them, too, if you add the extension) ...

Good to know! I like it because I don't have to switch headspace anymore when working on frontend and backend code.