Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues using writeJson with Babel #157

Closed
donaldpipowitch opened this issue Jul 29, 2015 · 4 comments
Closed

Issues using writeJson with Babel #157

donaldpipowitch opened this issue Jul 29, 2015 · 4 comments

Comments

@donaldpipowitch
Copy link

If I writeJson my output isn't formatted. I think this is a problem with jsonfile? I use the most recent version.

console.log(fs.jsonfile); // {}
fs.jsonfile.spaces = 2;
console.log(fs.jsonfile); // {} ... I think this should be { spaces: 2 }
@jprichardson
Copy link
Owner

What is the output that you get from this snippet?

var fs = require('fs-extra')

var data = { name: 'jp' }
fs.jsonfile.spaces = 4
fs.writeJson('/tmp/test.json', data, function (err) {
  if (err) console.error(err)
  var text = fs.readFileSync('/tmp/test.json', 'utf8')
  console.log(text)
})

@donaldpipowitch
Copy link
Author

Well... the output is correct:

{
    "name": "jp"
}

To be honest, I really use fs-extra-promise not vanilla fs-extra. So I changed your example after the first test to this:

var fs = require('fs-extra-promise')

var data = { name: 'jp' }
fs.jsonfile.spaces = 4
fs.writeJsonAync('/tmp/test.json', data).then(function () {
  var text = fs.readFileSync('/tmp/test.json', 'utf8')
  console.log(text)
})

And... the output is still correct:

{
    "name": "jp"
}

Now I was really confused... why doesn't it work for me outside of this small test case? I use fs-extra-promise instead of fs-extra, because I use Babel. So I made a third test:

import fs, { writeJsonAsync, readFileSync} from 'fs-extra-promise';

var data = { name: 'jp' }
fs.jsonfile.spaces = 4
writeJsonAync('/tmp/test.json', data).then(function () {
  var text = freadFileSync('/tmp/test.json', 'utf8')
  console.log(text)
})

And... BAM... this is the error:

{"name":"jp"}

The same happens if I use writeJson with fs-extra instead of the promisified version.

So... if Babel got the spec right (cc @sebmck) and doesn't have a bug, I'd say this is a bug on fs-extras side. As ES6 modules are now standardized (just not implemented yet in native Node) I'd expect import { writeJson } from 'fs-extra'; to work like var writeJson = require('fs-extra').writeJson.

@RyanZim RyanZim changed the title writeJson isn't formatted Issues using writeJson with Babel Oct 26, 2016
@RyanZim
Copy link
Collaborator

RyanZim commented Oct 26, 2016

@jprichardson Any ideas? I've got other more pressing things ATM.

@jprichardson
Copy link
Owner

This is over a year old. If there is still some issue with the latest versions and Babel6, feel free to reopen @donaldpipowitch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants