can't resolve Cannot find module 'rxjs' within typescript/webpack
Asked Answered
H

4

18

I've got a problem to load rxjs into a simple webpack setup (without angular). I'm running:

./node_modules/.bin/webpack --config webpack.config.js --watch

to start webpack. The only file with the app, src/app.ts, starts with:

import { Observable } from 'rxjs';

and this line is highlighted in VSCode and in webpack console with this error:

Cannot find module 'rxjs'.

But the overall output works fine. Just this console error.

tsconfig.json:

{
    "compilerOptions": {
         "target": "es2015"
    },
    "files": [
        "src/app.ts"
    ]
}

webpack.config.js:

module.exports = {
    entry: "./src/app.ts",
    output: {
        filename: "dist/bundle.js"
    },
    resolve: {
        // Add '.ts' and '.tsx' as a resolvable extension.
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },
    module: {
        loaders: [
            // all files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'
            { test: /\.ts?$/, loader: "ts-loader" }
        ]
    }
}

package.json (everything installed properly):

{
  "scripts": {
    "start": "./node_modules/.bin/webpack --config webpack.config.js --watch"
  },
  "devDependencies": {
    "ts-loader": "^2.1.0",
    "typescript": "^2.3.4",
    "webpack": "^2.6.1",
    "webpack-dev-server": "^2.4.5"
  },
  "dependencies": {
    "bootstrap": "^3.3.7",
    "rxjs": "^5.4.0"
  }
}

I can't find the reason for why can't ts/webpack resolve where is rxjs

Heronry answered 5/6, 2017 at 20:54 Comment(0)
A
21

You should change default module resolution strategy from Classic to Node. In order to do this, change your tsconfig.json file:

{
    ...
    "compilerOptions": {
        ....
        "moduleResolution": "node"
    }
}

See documentation on Module Resolution

Anticatalyst answered 5/6, 2017 at 23:56 Comment(2)
Well I don't know about OP but you saved me some hours :D Thanks!Grillage
It worked for me with "module": "commonjs" and updated VSCode to the latest version. But if it helped someone - it's good :)Heronry
P
16

to ionic

simple use this command in order to delete the plugin

npm uninstall --save rxjs

npm uninstall --save rxjs-compat

and then run this to reinstall

npm install --save rxjs

npm install --save rxjs-compat
Presuppose answered 12/6, 2018 at 20:31 Comment(0)
W
3

I had this issue when I tried building with nestjs from scratch, this should solve your problem

npm install rxjs

and if that doesn’t, try this:

delete node_modules and package-lock.json
rm -rf node_modules rm -f package-lock.json

clean npm cache
npm cache clean --force

try reinstalling
npm install

Watery answered 27/12, 2022 at 0:14 Comment(0)
A
0

You must add node_modules to your webpack resolve.modules config:

 resolve: {
    modules: [
      "node_modules",
      path.resolve(__dirname, "app")
    ],
    extensions: [".js", ".json", ".jsx", ".css"],
 }

more info on https://webpack.js.org/configuration/resolve/

Arand answered 5/6, 2017 at 23:54 Comment(1)
this doesn't make much sense to me; the docs say that resolve.modules defaults to "node_modules", so I don't have to add it, I just don't change it. And I don't have to add "app" there, because why?Heronry

© 2022 - 2024 — McMap. All rights reserved.