0

const csv = require('csvtojson') this works

import { csv } from 'csvtojson' but this dont.

Can anyone help what the issue here ?

Shubham Singh
  • 199
  • 1
  • 3
  • the ìmport` works in the latest version of node. I think the support started around node version 13.3 or so – Andre Jul 30 '20 at 08:54

2 Answers2

0

The {} is to import partially from the csvtojson lib, and require is used to import every exported object from lib. What kind of error do you get? Could it be that later in your code you depend on something else from csvtojson and that is why you get an error.

Consider the example below:

 // hello.js
    function hello1() {
      return 'hello1'
    }
    function hello2() {
      return 'hello2'
    }
    export { hello1, hello2 }
    
    // app.js
    import { hello1, hello2 } from './hello'
    hello1()  // returns hello1
    hello2()  // returns hello2
0

You would have to use .mjs file extention instead of .js.

Read about ECMAScript modules enabling - it is still experimental so you may want to be sure before using it.

Another way would be to use babel to compile code to commonJS

fedesc
  • 2,554
  • 2
  • 23
  • 39