0

Attempting to generate a package.json with node and it works if I define the function like this:

const generatePackageJson = (name) => {
    const package = 
    `
    {
        "name": "${name}",
    }
    `;
    return package;
}

The package string returned contains the name parameter. However doing this does not work:

const generatePackageJson = (name) => {
    return
    `
    {
        "name": "${name}",
    }
    `;
}

The result returned is undefined. Just curious as to why this is?

Ole
  • 29,797
  • 32
  • 110
  • 232
  • 1
    Because you aren't returning the object, the interpreter is using automatic semicolon insertion, so it thinks you are doing `return;` instead of `return {}` – Patrick Evans Apr 23 '18 at 23:43
  • 1
    Your first example now works. In your second example, `return` can't be on the line by itself. You need to start the opening backpack on the same line. – Mark Apr 23 '18 at 23:47
  • AHA! Got it - Thanks. Moving the backtick to the same line as the return statement fixed it. – Ole Apr 23 '18 at 23:49
  • 1
    I returns undefined because in the line you return there is nothing, if you move the backtick to the line up it will return fine. – desoares Apr 23 '18 at 23:49

0 Answers0