Peng Xu
Peng Xu

Reputation: 21

Webpack parse module path. Module not found: Error: Can't resolve './internal/Observable' in

I have used webpack@4 & webpack-cli@3 to build typescript demo with [email protected], but get errors. The code is simple:

import {Subject} from "rxjs";
const stateChange = new Subject();

And I run npm build and get:

Module not found: Error: Can't resolve './internal/Observable' in 'D:\working\test\node_modules\rxjs\_esm5'

But the Observable.js is there, and then I add a Observable.ts beside, and webpack found the './internal/Observable' module, but still have other errors, Why webpack can not recognize .js files? I use ts-loader@8.

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "npx webpack --display-error-details --config webpack.config.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "lit-html": "^1.3.0",
    "lite-html": "^0.2.4",
    "ts-node": "^9.0.0"
  },
  "dependencies": {
    "rxjs": "^6.6.3",
    "ts-loader": "^8.0.11",
    "typescript": "^4.0.5",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12"
  }
}

tsconfig.json


{
    "compilerOptions":{
        "outDir": "dist",
        "target": "ES5",
        "inlineSourceMap": true,
        "inlineSources": true,
        "declaration": true,
        "declarationDir": "dist",
        "allowJs": true,
        "lib": ["ES6", "DOM"],
        "experimentalDecorators": true
    },
    "files": [
        "src/index.ts"
    ]
}

webpack.config.js

const path = require('path');
module.exports = {
    entry: {
        index: './src/index.ts'
    },
    module: {
        rules: [
            {
                test: /\.ts?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    output: {
        filename: '[name].min.js',
        path: path.resolve(__dirname, 'dist')
    },
    mode: ENV_PROD,
    resolve: {
        extensions: [ '.ts']
    }
}

Upvotes: 0

Views: 380

Answers (2)

Peng Xu
Peng Xu

Reputation: 21

I found the problem, the resolve.extensions option in webpack.config.js should contain '.js'. Thanks @Walid M

Upvotes: 2

Walid M.
Walid M.

Reputation: 15

Try using :

"moduleResolution": "node",

in your tsconfig.

Upvotes: 0

Related Questions