module.exports/require
- This is the older way that node would import files into one another.
- Use it with normal javascript files. This works whether you have a package.json or not
- Some node packages may not be compatible with this syntax. But it is still in use
- In your import file (script.js)
const tasks = require('./taskList.js)
- Your export file would look something like this
module.exports = {
tasks: [
// output content here
]
}
import/export
- This is the newer syntax and is focused on modules
- If you are in a node project,
"type: "module"should be in your package.json - Otherwise you can use this syntax by giving files the
.mjsfile extension - Vue and Nuxt use this style of imports
- import file (script.mjs)
import { tasks } from './taskList.mjs'
- export file (tasList.mjs)
export const tasks = [
// array here
]
- note that an array is not necessary, it is just the content type used in these examples