Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 847
ducin Avatar asked Jun 05 '17 20:06

ducin


1 Answers

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

like image 71
Oles Savluk Avatar answered Oct 02 '22 21:10

Oles Savluk