简体   繁体   English

我如何运行npm的示例

[英]How can I run an example of npm

Here is a nice example on how to convert a complete json to csv. 是一个很好的示例,说明了如何将完整的json转换为csv。

I installed the program using npm install json-2-csv 我使用npm install json-2-csv安装了程序

I tried to take a run at the example. 我试图以这个例子为例。 I made a js file but I can't run it. 我制作了一个js文件,但无法运行。 How can I run it to see in action how it works? 如何运行它以查看其工作原理?

It is the example provided for json-2-csv page. 这是为json-2-csv页面提供的示例。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title> </title>
    </head>

    <body>

    <script>
    var converter = require('json-2-csv');

var documents = [
    {
        Make: 'Nissan',
        Model: 'Murano',
        Year: '2013',
        Specifications: {
            Mileage: '7106',
            Trim: 'S AWD'
        }
    },
    {
        Make: 'BMW',
        Model: 'X5',
        Year: '2014',
        Specifications: {
            Mileage: '3287',
            Trim: 'M'
        }
    }
];

var json2csvCallback = function (err, csv) {
    if (err) throw err;
    console.log(csv);
};

converter.json2csv(documents, json2csvCallback);

    </script>

    </body>

</html>

Assuming you have node and npm installed... 假设您已经安装了nodenpm ...

You need to forget about the HTML markup. 您需要忘记HTML标记。 That's what node is complaining about. 这就是节点所抱怨的。

  • Create a folder called something like example . 创建一个名为example的文件夹。
  • In that folder, run npm install json-2-csv . 在该文件夹中,运行npm install json-2-csv
  • Create a file called example.js and paste in the following. 创建一个名为example.js的文件,然后粘贴以下内容。 Basically, just the script from your question... 基本上,只是您问题中的脚本...

 var converter = require('json-2-csv'); var documents = [ { Make: 'Nissan', Model: 'Murano', Year: '2013', Specifications: { Mileage: '7106', Trim: 'S AWD' } }, { Make: 'BMW', Model: 'X5', Year: '2014', Specifications: { Mileage: '3287', Trim: 'M' } } ]; var json2csvCallback = function (err, csv) { if (err) throw err; console.log(csv); }; converter.json2csv(documents, json2csvCallback); 

  • Save the file in that folder. 将文件保存在该文件夹中。
  • In that folder, run node example.js . 在该文件夹中,运行node example.js

That's it! 而已! The program will print out the data in CSV format. 该程序将以CSV格式打印数据。

Assuming your file is called myFile.js, run the following from terminal in the same directory as your JS file: 假设您的文件名为myFile.js,请从终端在与JS文件相同的目录中运行以下命令:

node myFile.js

This assumes you've installed NodeJS which you can install from NodeJS.org. 假设您已经安装了NodeJS,可以从NodeJS.org安装它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM