I started learning reactive programming in javascript using RxJS and it's my first time using node. I don't exactly know what I'm doing wrong. Here is the code:
const array = [
'1', 'Max', '2', '3', '4', '5', 'IoT', '6', '7', '8', '9'
];
const stream = Rx.Observable.from(array);
This is the error:
TypeError: Rx.Observable.from is not a function
This is what I've tried so far:
I've installed RxJS using node install rxjs, nothing else.
I've tried importing from directly using import:
import { from } from 'rxjs';
When I've tried importing from using import, I've gotten this error:
SyntaxError: Cannot use import statement outside a module.
I've tried to go in the package-lock.json file and add the type of the module to module, but I've gotten the same error:
This is what I've tried doing to the package-lock.json file:
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"rxjs": {
"version": "7.4.0",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.4.0.tgz",
"integrity": "sha512-7SQDi7xeTMCJpqViXh8gL/lebcwlp3d831F05+9B44A4B0WfsEwUQHR64gsH1kvJ+Ep/J9K2+n1hVl1CsGN23w==",
"requires": {
"tslib": "~2.1.0"
},
"type": "module"
},
"tslib": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.1.0.tgz",
"integrity": "sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==",
"type": "module"
}
}
}
Sources:
Cannot use import statement outside a module...
fromArray is deprecated, use from
The question is :
How can I use from in RxJS
Your import syntax seems correct, One shouldn't temper XXXX.lock.json file as its strictly for package management purpose.
May be this codeSandbox link could help you understand issue in your setup.