Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Can't resolve all parameters for Location: (?) in angular7 webpack app

I get the following error in my browser when running locally a node.js-angular7-webpack4.21 app that I created NOT using angular-cli:

Error: Can't resolve all parameters for Location: (?).

I have extensively researched the issue. First, I do not have any component, service or other named 'Location', so I have no idea the parameters of what entity the compiler is having a hard time resolving. Second, I have only 4 services in my entire app, none of which point at each other so this is not a circular dependency issue. Neither did I forget to put the '@Injectable()' decorator correctly in either service file. Really, after triple and octuple checking, this is not the issue.

Moreover, I have reproduced this issue with an empty angular6 project consisting of 0 services and only 1 component with just words inside a div in its template, in which case I get the same error only about 'ApplicationModule' instead of 'Location'. So even though I do not have webpack errors, I assume the problem is in my webpack configuration, hence I will put bellow the webpack.config.js and tsconfig.js files, and hopefully someone can find something extremely stupid about them.

Here is my tsconfig.json:

{
  "compilerOptions": {
  "target": "es5",
  "lib": ["dom", "es2017", "es5", "es6", "es7"],
  "outDir": "dist",
  "module": "commonjs",
  "moduleResolution": "node",
  "sourceMap": true,
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "removeComments": true,
  "noImplicitAny": false,
  "typeRoots": ["types"],
  "types": ["node"]
},
  "exclude": ["node_modules"]
}

And here is my webpack.config.js:

var webpack = require('webpack');
var helpers = require('./helpers');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path'); // needed ?

module.exports = {

    mode: 'development',

    devtool: 'source-map',

    entry: {
        'main': helpers.root('app/main.ts'),
        'vendor': helpers.root('vendor.ts'),
        'polyfills': helpers.root('polyfills.ts')
    },

    output: {
        path: helpers.root('dist'),
        filename: '[name].js'
    },

    resolve: {
        extensions: ['.ts', '.js']
    },

    module: {
        rules: [{
                test: /\.ts$/,
                use: [{
                    loader: 'awesome-typescript-loader',
                    options: {
                        configFileName: helpers.root('tsconfig.json')
                    }
                }, 'angular2-template-loader?keepUrl=true']
            },
            {
                test: /\.html$/,
                use: 'html-loader'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                use: 'file-loader?name=assets/[name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                use: [
                    // 'style-loader',
                    MiniCssExtractPlugin.loader,
                    // 'to-string-loader',
                    'css-loader'
                ],
                exclude: [helpers.root('stylesheets')]
            },
            {
                test: /\.css$/,
                use: 'raw-loader',
                include: [helpers.root('stylesheets')]
            }
        ]
    },

    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html'
        }),
        new MiniCssExtractPlugin({
            // Options similar to the same options in webpackOptions.output
            // both options are optional
            filename: "[name].css",
            chunkFilename: "main.css"
        }),
        new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and Windows
            // For Angular 5, see also https://github.com/angular/angular/issues/20357#issuecomment-343683491
            /\@angular(\\|\/)core(\\|\/)fesm5/,
            helpers.root('app'), // location of your src
            {
                // your Angular Async Route paths relative to this root drectory
            }
        )
    ]

};

Thank you guys.

like image 566
Elitzur Bar-Yehuda Avatar asked Oct 20 '18 12:10

Elitzur Bar-Yehuda


1 Answers

I had this issue when upgrade angular 6.1.10 to 7.0.6 and webpack 4.19.1. I did some changes described in this answer and it worked. To short:

Add to your polyfill.ts

import 'core-js/es7/reflect';

I hope this helps.

like image 73
Tony Avatar answered Nov 12 '22 11:11

Tony