Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue-router not working after npm run build

Tags:

webpack

vuejs2

I have a vue project and I have installed it using vue cli.

I have installed vue-router and all the routes works fine in npm run dev mode.

The URL is localhost:8000. After that I run npm run build command, and navigate to the url localhost/projet_name this deploying blank page and no routes is working.

I am using windows system and works on wampp server.

If I want to run the project using localhost/projet_name what should I do?

This is my folder structure after running the command npm run build.

https://i.stack.imgur.com/HeoTG.png

webpack.config.js file is:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

Packages.json file:

{
  "name": "posthere",
  "description": "A Vue.js project",
  "version": "1.0.0",
  "author": "Sandip",
  "license": "MIT",
  "private": true,
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "dependencies": {
    "vue": "^2.5.11",
    "vue-resource": "^1.3.5",
    "vue-router": "^3.0.1"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-stage-3": "^6.24.1",
    "cross-env": "^5.0.5",
    "css-loader": "^0.28.7",
    "file-loader": "^1.1.4",
    "vue-loader": "^13.0.5",
    "vue-template-compiler": "^2.4.4",
    "webpack": "^3.6.0",
    "webpack-dev-server": "^2.9.1"
  }
}
like image 918
Sandip Nag Avatar asked Jan 04 '18 13:01

Sandip Nag


People also ask

How do I refresh my Vue router?

To reload a route with Vue Route, we can call the this. $router.go() method. If it has no arguments, then it'll reload the current route. This way, it'll notice when the path changes and it'll trigger a reload of the component with new data.

How do I install Vue 3 Vue router?

Installing Vue Router for Vue 3 Like many frameworks, Vue has its own CLI. So at first, you need to install it using NPM or Yarn. Then you can create a new project using the vue create <project-name> command. After executing it, the CLI prompts with several options as follows.


2 Answers

You must configure your web server to redirect all requests to your index.html file.

For example, you must configure your .htaccess file like this for Apache server:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

You can also see other server configuration examples here:

like image 194
salihtopcu Avatar answered Sep 18 '22 18:09

salihtopcu


This issue exists in template. There is a pull request pending on github.Problem is with path mentioned for build.js in webpack.config.js and in index.html. You need to modify webpack.config.js

from

output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },

to

output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: './dist/',
    filename: 'build.js'
  },

and in index.html change <script src="/dist/build.js"></script> to <script src="./dist/build.js"></script>

like image 21
Talha Junaid Avatar answered Sep 18 '22 18:09

Talha Junaid