Home > OS >  How to import a list from other .js file to my main.js file?
How to import a list from other .js file to my main.js file?

Time:12-16

This is just an example from Ania kubow's Climate change API.

My question is : i have a list like this but it is more larger and long. I want to store that list in another file like "exampleList.js" then i want to import it to my main.js file.

const newspapers = [
    {
        name: 'cityam',
        address: 'https://www.cityam.com/london-must-become-a-world-leader-on-climate-change-action/',
        base: ''
    },
    {
        name: 'thetimes',
        address: 'https://www.thetimes.co.uk/environment/climate-change',
        base: ''
    },
    {
        name: 'guardian',
        address: 'https://www.theguardian.com/environment/climate-crisis',
        base: '',
    },
    {
        name: 'telegraph',
        address: 'https://www.telegraph.co.uk/climate-change',
        base: 'https://www.telegraph.co.uk',
    },
    {
        name: 'nyt',
        address: 'https://www.nytimes.com/international/section/climate',
        base: '',
    },
    {
        name: 'latimes',
        address: 'https://www.latimes.com/environment',
        base: '',
    },
]

Then i want to call it here instead of writing it in the same file (main.js) i dont want the code to look messy and too long. Actually i have more lists (probably 3 lists each has more than 100 address, base, name) i want to store them in different files.

app.get('/news/:newspaperId', (req, res) => {
    const newspaperId = req.params.newspaperId

    const newspaperAddress = newspapers.filter(newspaper => newspaper.name == newspaperId)[0].address
    const newspaperBase = newspapers.filter(newspaper => newspaper.name == newspaperId)[0].base


    axios.get(newspaperAddress)
        .then(response => {
            const html = response.data
            const $ = cheerio.load(html)
            const specificArticles = []

            $('a:contains("climate")', html).each(function () {
                const title = $(this).text()
                const url = $(this).attr('href')
                specificArticles.push({
                    title,
                    url: newspaperBase   url,
                    source: newspaperId
                })
            })
            res.json(specificArticles)
        }).catch(err => console.log(err))
})

I have tried to create a list file then i tried the statement import:

import exampleList from (./src/exampleList.js)

it says that i need to add "type": "module" to my package.json. i did that but it still not working, it says that i cannot import a statement from module . i also tried to run the app with .mjs and --node-experimental ... same thing, not working.

CodePudding user response:

Firstly, make sure you are exporting the list that you have. Also, I recommend to store the data in a JSON format.

Secondly, regarding the error that you are facing

add "type": "module" to package.json

check out the main answer on this question

CodePudding user response:

const newspapers = ["hello"]; module.exports = newspapers; use this to export your data from the file and use const newspaper = require("./src/exampleList") to import the file and can use the data in the other file.

  • Related