Search code examples
javascriptnode.jsundefinedmultilingual

Nodejs not recognizing string(Input in Japanese)


I am trying to take an input in Japanese language and pass the value as parameter to another function to retrieve data from the function but whenever I run node app.js it doesn't recognize the input. But in the browser it is working just fine.

So it is accessing data from json const innerArray = { Name : 'Test', Prefecture: '東京都' }

While accessing it:

let prefectureName = innerArray.Prefecture
console.log(prefectureName)

The output is ???

When I use the input in english it also works. Then I also tried to convert the japanese input into english. then again the same problem remains as it can not read the input. Can anyone help me regarding this matter?


Solution

  • I suspect this is only a problem in your log console. Node.js uses UTF-16 internally, so Japanese characters are fully supported.

    I'd suggest trying the following:

    const fs = require("fs");
    const innerArray = { Name : 'Test', Prefecture: '東京都' }
    fs.writeFileSync("test.json", JSON.stringify(innerArray), "utf8");
    console.log("innerArray:", innerArray);
    

    Then open test.json in something like Notepad++, you should see the characters rendered correctly.

    If I try this example in Visual Studio Code, the output is fine too since the console or output font has support for Japanese Characters.