Getting error trying to read a json file with babel/register module

2.4k Views Asked by At

this is my config file:

import nconf from "nconf";
import path from "path";

nconf.argv()
    .env()
    .file({
        file: path.join(
                __dirname,
                `manifest.${process.env['NODE_ENV'] || 'development'}.json`
            )
    });

var manifest = {
    server: nconf.get('server'),
    connections: nconf.get('connections'),
    plugins: nconf.get('plugins')
};

export default manifest;

and this is the config file

{
    "server": {},
    "connections": [
        {
            "port": 3000,
            "labels": ["api"]
        }
    ],
    "plugins": [
        {
            "vision": {},
            "visionary": {
                "engines": {
                    "jsx": "hapi-react-views"
                },
                "relativeTo": __dirname,
                "path": ""
            }
        }
    ]
}

unfortunately I get the following error:

throw new Error("Error parsing your configuration file: [" + self.file +
            ^
Error: Error parsing your configuration file: [/Users/mazzy/vagrant-devbox/hapi-react-es6/server/config/manifest.development.json]: Unexpected token _
    at [object Object].File.loadSync (/Users/mazzy/vagrant-devbox/hapi-react-es6/node_modules/nconf/lib/nconf/stores/file.js:14
1

There are 1 best solutions below

0
Jordan Running On

I believe the problem is here:

"relativeTo": __dirname,

nconf is assuming your config file is JSON, but this isn't valid JSON.

If you need JavaScript in your config file, one option is to require (or import) it and then use .defaults instead of .file (you'll probably want to change the filename from .json to .js to make it clear that it's JavaScript, not JSON):

import nconf from "nconf";
import path from "path";
import config from `./manifest.${process.env['NODE_ENV'] || 'development'}`;

nconf.argv()
    .env()
    .defaults(config);

// ...

I haven't tested this, but hopefully it helps.