NodeJS Передача значений переменных между файлами
Подскажите, как можно организовать в node js, что б значения переменной по ссылке скажем в файле
big-list.js
export let bigData = [
'https://google.com',
'https://habr.com/',
'https://ru.stackoverflow.com/',
];
было доступно в другом файле, скажем server.js
const bigList = require("big-list.js");
function getResult(){
return console.log(bigList);
}
getResult();
Что не так в синтаксисе? Почему выдает ошибку Cannot use import statement outside a module ?
Ответы (1 шт):
Автор решения: Aleksandr Belous
→ Ссылка
В nodejs по умолчанию экспортирование идет так:
big-list.js
module.exports = [
'https://google.com',
'https://habr.com/',
'https://ru.stackoverflow.com/',
];
server.js
const bigList = require("big-list.js");
console.log(bigList);
Но вы можете добавить в package.json опцию "type": "module", чтобы использовать import/export:
big-list.js
export default [
'https://google.com',
'https://habr.com/',
'https://ru.stackoverflow.com/',
];
server.js
import bigList from "big-list.js";
console.log(bigList);