Import Local JSON In Node.js v8.5 Experimental Modules
Tags: node, javascript, modules
Published: 2017-09-16 21:46:26

---
cover_image: https://thepracticaldev.s3.amazonaws.com/i/aszgvu6be3pcp2gaccw2.jpg
title: Import Local JSON In Node.js v8.5 Experimental Modules
published: true
description: Require local JSON files with `require()` while using #nodejs v8.5 experimental modules!
tags: node,javascript,modules
---

In case you missed it, Node.js v8.5.0 was released on September 12, bringing a host of [new features](https://nodejs.org/en/blog/release/v8.5.0/) and fixes. 

Among these features, and one I'm quite excited about, is (experimental) support for ES2015+ modules in Node. In order to use "ESModules", one must:

* Convert all Node files to use the `.mjs` extension
* Change all `require()` and `module.exports` statements to `import` and `export` statements, respectively
* Run any Node files/applications using these modules under the `--experimental-modules` flag 

[This article](http://2ality.com/2017/09/native-esm-node.html) by Dr. Axel Rauschmayer has more details on the new system, as well as some examples and potential issues to watch for.

That's all good, but what if you're importing local data or information from a `package.json` file? That won't work if you're using the `import` statement, since the import does not parse files the same as the `require` function, and JSON files do not use the necessary `.mjs` file extension.

You could move all your JSON files over to Javascript object syntax inside `.mjs` files, or you could use a [snippet I wrote](https://github.com/geoffdavis92/require.mjs/blob/master/snippet.js) to use the `fs` module to import the data. In this case, you do not need to make any updates to the JSON files that already use `require()`, as the `require` function in the snippet has the same result as the Node function.

Here is a sample application before using standard CommonJS modules:

```javascript
// package.json
{
  // ...
  "version": "1.0.0",
  // ...
}

// logVersion.js
const logVersion = (version) => console.log(`Hello World v${version}`);
module.exports = { logVersion }

// index.js
const { version } = require('./package.json');
const logVersion = require('./logVersion.js');

logVersion(version); // -> Hello World v1.0.0
```

…And the same application using the Experimental Modules and my `require()` snippet:

```javascript
// package.json
{
  // ...
  "version": "1.0.0",
  // ...
}

// logVersion.js
const logVersion = (version) => console.log(`Hello World v${version}`);
export default logVersion

// index.js
import fs from 'fs';
import logVersion from './logVersion';

const require = (filepath, encoding = "utf8") =>
	JSON.parse(fs.readFileSync(filepath, { encoding }));

const { version } = require('./package.json');

logVersion(version); // -> Hello World v1.0.0
```

Eventually, I would like to create a module, but have run into some issues with resolving paths in modules; if you would like to help debug and/or develop this functionality, feel free to file a [pull request](https://github.com/geoffdavis92/require.mjs/pulls) or an [issue](https://github.com/geoffdavis92/require.mjs/issues/new).

My snippet on Github can be found here: [geoffdavis92/require.mjs](https://github.com/geoffdavis92/require.mjs/blob/master/snippet.js).

Enjoy!