question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does not work - produced paths for require are incorrect.

See original GitHub issue

Please, observe the following TypeScript file:

import { Component } from "@angular/core";

@Component({
    selector: 'app-root',
    templateUrl: './app/app.component.html'
})
export class AppComponent {
}

And it works fine before I use angular2-template-loader. When I am trying to use the latter and run webpack it fails with the following message:

    polyfills.js     283 kB       1  [emitted]  polyfills
       vendor.js    3.32 MB       2  [emitted]  vendor
     main.js.map    17.2 kB       0  [emitted]  main
polyfills.js.map     353 kB       1  [emitted]  polyfills
   vendor.js.map    3.94 MB       2  [emitted]  vendor
      index.html  607 bytes          [emitted]
    + 912 hidden modules

ERROR in ./src/client/app/app.component.ts
Module not found: Error: Cannot resolve 'file' or 'directory' ./app/app.component.html in C:\Users\markk\IdeaProjects\QuestionarySample2\src\client\app
 @ ./src/client/app/app.component.ts 18:22-57

...
Child html-webpack-plugin for "index.html":
        + 1 hidden modules

C:\Users\markk\IdeaProjects\QuestionarySample2>

(there are more errors like that for other components, I replaced them with an ellipsis)

The loader converts the aforementioned source into:

import { Component } from "@angular/core";

@Component({
    selector: 'app-root',
    template: require('./app/app.component.html')
})
export class AppComponent {
}

And now it fails.

As I understand, the reason is that require works off the current directory, whereas the templateUrl is relative to the root. So, β€˜./app/app.component.html’ is correct when taken relative to the root, but wrong when relative to the current location.

It is worth noting, that I do not use require anywhere in the source code, only the ES6 import statement, which is also relative to the current location. It is only the Angular2 templateUrl and styleUrls that are relative to the root.

Anyway, I must be doing something wrong, because nobody seems to have my problem. But what am I doing wrong?

The file name is src/client/app/app.component.ts. The source code directory structure is:

C:.
β”‚   .gitignore
β”‚   karma.conf.js
β”‚   package.json
β”‚   tsconfig.json
β”‚   tslint.json
β”‚   typings.json
β”‚   webpack.config.js
β”‚           
β”œβ”€β”€β”€config
β”‚       karma-test-shim.js
β”‚       karma.conf.js
β”‚       webpack.common.js
β”‚       webpack.dev.js
β”‚       webpack.prod.js
β”‚       webpack.scratch.js
β”‚       webpack.test.js
β”‚       
└───src
    β”œβ”€β”€β”€client
    β”‚   β”‚   global.css
    β”‚   β”‚   index.html
    β”‚   β”‚   main.ts
    β”‚   β”‚   polyfills.ts
    β”‚   β”‚   tsconfig.json
    β”‚   β”‚   vendor.ts
    β”‚   β”‚   
    β”‚   └───app
    β”‚       β”‚   app.component.html
    β”‚       β”‚   app.component.ts
    β”‚       β”‚   app.module.ts
    β”‚       β”‚   app.routing.ts
    β”‚       β”‚   settings.component.ts
    β”‚       β”‚   signout.component.ts
    β”‚       β”‚   
    β”‚       β”œβ”€β”€β”€assets
    β”‚       β”‚       ...
    β”‚       β”‚       
    β”‚       β”œβ”€β”€β”€questionnaire
    β”‚       β”‚       ...
    β”‚       β”‚       
    β”‚       └───shared
    β”‚               ...
    β”‚               
    └───server
            api.ts
            main.ts
            tsconfig.json         

The webpack configuration is:

webpack.config.js

module.exports = require('./config/webpack.dev.js');

./config/webpack.dev.js

var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
const path = require('path');

module.exports = webpackMerge(commonConfig, {
    // devtool: 'cheap-module-eval-source-map',
    devtool: 'source-map',

    output: {
        publicPath: `http://localhost:${commonConfig.port}/`,
        filename: '[name].js',
        sourceMapFilename: '[name].js.map',
        chunkFilename: '[id].chunk.js'
    },

    plugins: [
        new ExtractTextPlugin('[name].css')
    ],

    devServer: {
        inline: true,
        hot: true,
        progress: true,
        port: commonConfig.port,
        proxy: {
            '/api*': {
                target: `http://localhost:${commonConfig.port - 1}`
            }
        },
        historyApiFallback: true,
        watchOptions: {
            aggregateTimeout: 300,
            poll: 1000
        }
    }
});

./config/webpack.common.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');

module.exports = {
    port: process.env.PORT || 3000,

    entry: {
        polyfills: './src/client/polyfills',
        vendor: './src/client/vendor',
        main: './src/client/main'
    },

    output: {
        path: path.join(__dirname, '../dist/client'),
    },

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

    module: {
        loaders: [
            {
                test: /\.ts$/,
                loaders: ['ts', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                // loader: 'html'
                loader: 'raw'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file?name=assets/[name].[hash].[ext]'
            },
            // {
            //     test: /\.css$/,
            //     exclude: path.join(__dirname, '../src/client/app'),
            //     loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
            // },
            {
                test: /\.css$/,
                // exclude: path.join(__dirname, '../src/client/app'),
                loader: 'raw'
            }
        ]
    },

    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: ['main', 'vendor', 'polyfills']
        }),

        new HtmlWebpackPlugin({
            template: 'src/client/index.html'
        })
    ]
};

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sujithuixcommented, Mar 3, 2017

Anyone found a solution for this? spent days with no luck. Any input will be appreciated.

0reactions
ajohnsonRHcommented, Nov 2, 2016

Has someone found a fix for this yet?

Read more comments on GitHub >

github_iconTop Results From Across the Web

wrong relative path resolution Β· Issue #1395 - GitHub
I can reproduce the issue. It seems to be a problem adressing a file above the baseUrl. I have the following solution structure:...
Read more >
what's wrong with my require_once path? - Stack Overflow
You use require_once and have error "Class 'Facebook' not found". If you tried require_once on a file that does not exist, it would...
Read more >
11 Ways to Fix "The System Cannot Find The Path Specified ...
Here are eleven fixes you can do to solve this problem right away. 1. Run Command Prompt as an Administrator. Windows utilities cannot...
Read more >
Test-Path - PowerShell - Microsoft Learn
The Test-Path cmdlet determines whether all elements of the path exist. It returns $True if all elements exist and $False if any are...
Read more >
Setting False Path Exceptions - Xilinx
In some cases, they are essential to making the site work properly. By accessing this site, you direct us to use and consent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found