here iam adding codes of 2 files. first one is notes.js and second one id index.js.. i dont know how to import export in newer version of node.js.here iam adding codes of 2 files. first one is notes.js and second one id index.js.. i dont know how to import export in newer version of node.js.here iam adding codes of 2 files. first one is notes.js and second one id index.js.. i dont know how to import export in newer version of node.js.here iam adding codes of 2 files. first one is notes.js and second one id index.js.. i dont know how to import export in newer version of node.jshere iam adding codes of 2 files. first one is notes.js and second one id index.js.. i dont know how to import export in newer version of node.js
//this is my nottes.js :
import fs from 'fs';
const getNotes = function(){
return 'yours notes....'
}
const addNote = function(title,body){
const note = loadNotes()
}
const loadNotes = function(){
const dataBuffer = fs.readFileSync('note.json')
const dataJson = dataBuffer.toString()
return JSON.parse(dataJson)
}
module.exports={
addNote:addNote,
getNotes:getNotes
}
//this is my index.js:
import yargs from 'yargs';
import notes from './notes.js'
//const notes = require('./notes');
import {hideBin} from 'yargs/helpers';
const argv = yargs(process.argv.slice(2));
yargs(hideBin(process.argv)).command({
command:'add',
describe:'add a new note',
builder:{
title:{
describe:"note title",
demandOption :true,
type:'string'
},
body:{
describe:"note body",
demandOption :true,
type:'string'
},
tile:{
describe:"note tile",
demandOption :true,
type:'string'
}
},
handler:function(argv){
notes.addNote(argv.title,argv.body)
},
})
yargs(hideBin(process.argv)).command({
command:'remove',
describe:'removing a new note',
handler:function(){
console.log('removing a new note!')
}
}).parse()
//console.log(argv.argv);
argv.parse()
Try to add the export keyword before the functions in notes.js and remove the module.exports:
export const getNotes = ...
export const addNote = ...
export const loadNotes = ...
You should then be able to import the function with
import {
getNotes,
addNote,
loadNotes
} from './notes'
For more information about exporting in ES6 you can check this doc from MDN.