Ask coding questions

← Back to all posts
Requiring rxjs via Node/NPM not working correctly
h
Manish_Giri (0)

I have been trying to learn Observables using Rx.js and wanted to give it a shot on Repl, to avoid setting up my local dev environment. However, since "import" doesn't work on Repl, and the Rx.js website doesn't show usage with anything other than using "import", I'm stuck.

Here is the simple code snippet I'm trying to run, copied from their guide.

import { of } from 'rxjs';
import { map } from 'rxjs/operators';

of(1, 2, 3).pipe(map(x => x + '!!!')); // etc

I tried to translate this using require, and tried out various variations of the map operator, but I keep getting an error with it.

const rx = require('rxjs');
const of = rx.of;
const map = rx.operators.map;
//const map = rx.map;
const pipe = rx.pipe;

of(1, 2, 3).pipe(map(x => x + '!!!')); 

I keep getting some error or the other with the map operator -

TypeError: Cannot read property 'map' of undefined
    at /home/runner/AmusedWrongLinuxpc/index.js:6:26
    at Script.runInContext (vm.js:130:18)
    at Object.<anonymous> (/run_dir/interp.js:209:20)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

Of course, I do have rxjs installed via npm, using the "Packages" tab on the left. Here is the relevant snippet from package.json -

  "dependencies": {
    "rxjs": "^7.1.0"
  }

Any help?