webpack import firebase not working - npm

I'm having an issue getting firebase 3.0.1 to work. I have a feeling it's in regards to my webpack setup. My files are below. When running my app with webpack dev server I get the error:
Uncaught TypeError: firebase.initializeApp is not a function
The interesting thing is that if I put a debugger; or breakpoint after var firebase = require('firebase'); it seems to be an empty object.
webpack.config.js
const webpack = require("webpack");
module.exports = {
entry: './src/index.js',
output: {
path: 'public',
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader?presets[]=es2015&presets[]=react'
}]
},
plugins: process.env.NODE_ENV === 'production' ? [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin()
] : []
};
package.json
{
"name": "burn",
"version": "1.0.0",
"description": "burn messaging",
"main": "index.js",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:dev": "webpack-dev-server --inline --content-base public --history-api-fallback",
"start:prod": "webpack && firebase deploy"
},
"author": "James Gilchrist <james#burn.today>",
"license": "ISC",
"dependencies": {
"compression": "^1.6.2",
"express": "^4.13.4",
"firebase": "^3.0.1",
"if-env": "^1.0.0",
"react": "^15.0.2",
"react-dom": "^15.0.2",
"react-router": "^2.4.0"
},
"devDependencies": {
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.14.1"
}
}
index.js
var firebase = require('firebase');
var config = {
apiKey: "AIzaSyA9gUmSBu4SZ4P9H_4lXuN1ouD_GBKq3aw",
authDomain: "burn-56840.firebaseapp.com",
databaseURL: "https://burn-56840.firebaseio.com",
storageBucket: "burn-56840.appspot.com"
};
firebase.initializeApp(config);

I had the same problem, there's a simple fix though:
var firebase = require('firebase/app');
This way you get the "real" firebase module. However you must now require each module you'll need so it loads correctly, like so:
var firebase = require('firebase/app');
// all 3 are optional and you only need to require them at the start
require('firebase/auth');
require('firebase/database');
require('firebase/storage');
It seems to me that something is wrong with the current initialisation code, looking at the source it should work; but then again, somewhat like you, I'm using browserify, and haven't tested outside of it, so it might be related.

Related

Missing Dependency: aws-sdk in ./node_modules/node-pre-gyp/lib/info.js

I have a new Vue/Electron app just built using vue-cli-plugin-electron-builder and which will utilize sqlite.
The base install runs fine in development until I run yarn add sqlite3. I'm then presented with the aws-sdk missing dependency error.
Research suggests it's a webpack issue that can be fixed using webpack externals.
And, from the vue-electron-builder docs a similar explanation and fix.
I've updated my vue.config.js and webpack.config.js files based on this but am a few hours in now without resolution.
Any help or suggestions appreciated. Thanks.
[webpack node externals thread on github] (https://github.com/liady/webpack-node-externals)
Reading on webpack with backend apps
// vue.config.js
module.exports = {
pluginOptions: {
electronBuilder: {
// List native deps here if they don't work
externals: [ 'sqlite3' ],
},
// If you are using Yarn Workspaces, you may have multiple node_modules folders
// List them all here so that VCP Electron Builder can find them
nodeModulesPath: ['../../node_modules', './node_modules']
}
}
//webpack.config.js
// this file is for cases where we need to access the
// webpack config as a file when using CLI commands.
const nodeExternals = require('webpack-node-externals');
let service = process.VUE_CLI_SERVICE
if (!service || process.env.VUE_CLI_API_MODE) {
const Service = require('./lib/Service')
service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd())
service.init(process.env.VUE_CLI_MODE || process.env.NODE_ENV)
}
module.exports = {
service.resolveWebpackConfig(),
externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc.
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
}
//package.json
{
"name": "spectral",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"postuninstall": "electron-builder install-app-deps"
},
"main": "background.js",
"dependencies": {
"axios": "^0.21.1",
"core-js": "^3.6.5",
"sqlite3": "^5.0.0",
"vue": "^2.6.11",
"vue-router": "^3.2.0",
"webpack-node-externals": "^2.5.2"
},
"devDependencies": {
"#vue/cli-plugin-babel": "^4.5.0",
"#vue/cli-plugin-eslint": "^4.5.0",
"#vue/cli-plugin-router": "^4.5.9",
"#vue/cli-service": "^4.5.0",
"babel-eslint": "^10.1.0",
"electron": "^9.0.0",
"electron-devtools-installer": "^3.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^6.2.2",
"vue-cli-plugin-electron-builder": "^2.0.0-rc.5",
"vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
//sqlite connection setup
const path = require('path');
const sqlite3 = require('sqlite3').verbose()
const dbPath = path.resolve('src/db/spectral.db');
let db
export const conn = () => {
if (!db || !db.open) {
db = new sqlite3.Database(dbPath)
}
return db
}
More research led me to this thread which solved the 'sqlite3 not defined error' but created a new error 'require not found' which required adding nodeIntegration = true into vue.config.js.
// working solution vue.config.js
module.exports = {
pluginOptions: {
electronBuilder: {
externals: ['sqlite3'],
nodeIntegration: true
},
}
};
Additional references
vue-electron-builder native modules

How to fix chunk load failure?

I am trying to make a web application using Vue and Monaco Editor for the frontend and Asp.Net Core 3 on the backend.
I am also using webpack since I'm using Vue single page components.
I am new to webpack, and don't understand all its functionality. Anyway, webpack splits the build into several files (chunks it seems). However, when loading the web page I keep getting the error Uncaught (in promise) ChunkLoadError: Loading chunk 2 failed.
I tried to googling the answer, but nothing I have done has worked so far.
Here are my package.json and webpack.config.json.
Package.json
{
"name": "parvis",
"version": "1.0.0",
"description": "This is a description",
"main": "main.js",
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"serve": "cross-env NODE_ENV=development webpack --devtool source-map --progress --hide-modules",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"keywords": [
"dataflow"
],
"author": "Marin Aglić Čuvić",
"license": "MIT",
"dependencies": {
"bootstrap": "^4.3.1",
"bootstrap-vue": "^2.0.2",
"escodegen": "^1.12.0",
"filbert": "^0.1.20",
"lodash": "^4.17.15",
"monaco-editor": "^0.18.1",
"monaco-editor-webpack-plugin": "^1.7.0",
"vee-validate": "^3.0.8",
"vue": "^2.6.10",
"vue-monaco": "^1.1.0",
"vue-router": "^3.1.3"
},
"devDependencies": {
"#babel/core": "^7.6.2",
"#babel/preset-env": "^7.6.2",
"babel-loader": "^8.0.6",
"cross-env": "^6.0.3",
"css-loader": "^3.2.0",
"html-webpack-plugin": "^3.2.0",
"rimraf": "^3.0.0",
"uglifyjs-webpack-plugin": "^2.2.0",
"vue-loader": "^15.7.1",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.6.10",
"webpack": "^4.41.0",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.8.2"
}
}
This is my webpack.config.js.
const path = require('path');
const webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
module.exports = {
mode: process.env.NODE_ENV,
entry: {
main: './wwwroot/client/src/main.js',
// 'editor.worker': 'monaco-editor/esm/vs/editor/editor.worker.js'
},
output: {
globalObject: 'self',
path: path.resolve(__dirname, './wwwroot/vuebundles/'),
publicPath: '/wwwroot/vuebundles/',
filename: '[name].build.js',
chunkFilename: '[name].chunk.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
}, {
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
},
]
},
plugins: [
new VueLoaderPlugin(),
new MonacoWebpackPlugin({
languages: ['javascript', 'csharp']
})
]
};
I understand similar questions have been posted in the past, but most of them have not been answered. Any solution I tried didn't work for me.
It took me an embarrassingly long time to figure out that my publicPath was wrong.
Probably because I didn't use it from webpack before it started creating chunks.
Anyway, the publicPath property should be:
publicPath: '/vuebundles/',
Without "wwwroot".

Issue using extract-text-webpack-plugin

I am trying to use the Extract Text Webpack Plugin, but I have not been able to generate a new css file for the compiled sass files.
webpack.config.js
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry : './src/app.js',
output : {
filename : './dist/bundle.js'
},
module: {
rules: [
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader','sass-loader']
})
}
]
},
plugins: [
new ExtractTextPlugin({
filename : 'app.bundle.css'
}),
]
}
package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"dev": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^0.28.9",
"extract-text-webpack-plugin": "^3.0.2",
"node-sass": "^4.7.2",
"sass-loader": "^6.0.6",
"style-loader": "^0.20.1",
"webpack": "^3.11.0",
"webpack-dev-server": "^2.11.1"
}
}
app.js:
var css = require('../css/app.scss');
console.log("This is a test Hi from webpack !!!");
I able to generate a new css file for the compiled sass files
please go through repo for your reference working example
your configuration looks good only ,my be you miss the path or forgot load css file in index.html or may be you did not create any scss files
live example

create-react-app proxy works only with fetch api but not in simple get of the browser

I'm using pretty small create-react-app running on port 3000 setup to proxy requests to backend server running on port 8080.
If I put in the browser address bar http://localhost:3000/api/me I get back the index.html page but if I use fetch API to get /api/me it try to call to by backend.
The problem is that I have to authenticate with the backend which will set a cookie but since I can't access the login page on http://localhost:3000/login I can't get the cookie.
On a different project which I've eject from create-react-app I have small file to run webpack-dev-server wih the configuration
proxy: {
"*": "http://localhost:9191"
}
which does proxy requests even when put into the browser address bar.
Is it possible to create such setup in create-react-app?
Closer look into create-react-app code reveal that this is by design:
For single page apps, we generally want to fallback to /index.html.
However we also want to respect proxy for API calls.
So if proxy is specified, we need to decide which fallback to use.
We use a heuristic: if request accepts text/html, we pick /index.html.
Modern browsers include text/html into accept header when navigating.
However API calls like fetch() won’t generally accept text/html.
If this heuristic doesn’t work well for you, don’t use proxy.
Running GET of http://localhost:3000/api/me inside REST Console extension return the correct result.
Further reading about Fetch API and cookies reveal that I have to include the parameter credentials:true to send cookies:
fetch('/api/me', {
credentials: 'include'
})
Yes it is possible:
"proxy": {
"/api": {
"target": "<url>",
"ws": true
}
},
See https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#configuring-the-proxy-manually
I have my own react app and I tried adding proxy to the package.json, but it is nit working.
The simple create-react-app works just fine bu tit does not have any webpack configured.
Here is my webpack:
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve('./dist'),
filename: 'index_bundle.js'
},
module: {
loaders:[
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.jsx$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
loader: "style-loader!css-loader?modules"
},
{
test: /\.png$/,
loader: "url-loader?limit=100000"
},
{
test: /\.jpg$/,
loader: "file-loader"
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
}
]
},
resolve: {
extensions: ['.js', '.jsx' ,'.json'],
modules: [path.join(__dirname, 'src'), 'node_modules']
},
plugins: [HtmlWebpackPluginConfig]
}
My package.json:
{
"name": "A2ZPressMaterial",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --history-api-fallback",
"webpack-watch": "webpack -w",
"express-server": "node ./server",
"dev": "concurrently --kill-others \"npm run webpack-watch\" \"npm run express-server\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
"proxy": "http://localhost:3001",
"dependencies": {
"classnames": "^2.2.5",
"css-loader": "^0.28.7",
"file-loader": "^0.11.2",
"html-webpack-plugin": "^2.30.1",
"http-proxy-middleware": "^0.17.4",
"material-ui": "^0.19.2",
"path": "^0.12.7",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-lazyload": "^2.2.7",
"react-redux": "^5.0.6",
"react-router-dom": "^4.2.2",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"style-loader": "^0.18.2",
"url-loader": "^0.5.9",
"webpack": "^3.5.6",
"webpack-dev-server": "^2.8.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"concurrently": "^3.5.0"
}
}
I get 404 on my ajax/fetch calls
Yes it is possible. You will need to use http-proxy-middleware and configure proxy manually.
First install http-proxy-middleware in your react app
npm install http-proxy-middleware --save
Then create src/setupProxy.js file with below configuration
/* eslint-disable #typescript-eslint/no-var-requires */
/* eslint-disable no-undef */
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:9191',
secure: false,
changeOrigin: true,
})
);
};
Now simply start the application and you will see that non AJAX calls (document etc) are also being proxied correctly.
Note: Remove the proxy configuration from package.json file before configuring it manually with http-proxy-middleware otherwise the proxy won't work correctly if defined at both the places

Trying to compile Vue.js code using webpack - error

So I am trying to compile some vue.js code using webpack, but when I try to run webpack from command prompt I get the following error:
ERROR in Cannot find module 'core-js/library/fn/symbol/iterator'.
I can't get my head around this error and I don't know if I'm missing any dependencies.
Please let me know if there's something that's not right.
Here's my webpack.config:
module.exports = {
// This is the "main" file which should include all other modules
entry: './view/vue/main.js',
// Where should the compiled file go?
output: {
path: './layout/standard/js',
filename: 'vue-build.js'
},
module: {
// Special compilation rules
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
},
{
test: /\.vue$/,
loader: 'vue'
}
]
},
vue: {
loaders: {
js: 'babel'
}
}
}
And package.json:
{
"author": "CDG",
"license": "ISC",
"private": true,
"scripts": {
"prod": "gulp -p | webpack -p",
"dev": "gulp watch | webpack --watch"
},
"dependencies": {
"vue": "^1.0.27",
"vue-clickaway": "^1.1.3",
"vue-resource": "^0.9.3",
"vue-socket.io": "^1.0.2",
"vue-spinner": "^1.0.2",
"vuex": "^1.0.0-rc.2"
},
"devDependencies": {
"babel-core": "^6.1.2",
"babel-loader": "^6.1.0",
"babel-plugin-transform-runtime": "^6.1.2",
"babel-preset-es2015": "^6.14.0",
"babel-preset-stage-0": "^6.1.2",
"babel-runtime": "^5.8.0",
"css-loader": "^0.23.0",
"style-loader": "^0.13.0",
"vue-html-loader": "^1.0.0",
"vue-loader": "^7.3.0",
"webpack": "^1.12.2"
}
}
And .babelrc:
{
"presets": ["es2015", "stage-0"],
"plugins": ["transform-runtime"]
}
Please help me with this problem, it's really bugging me.
Thank you!
EDIT:
I did manage to solve the issue running npm i core-js, but now I have the following error when typing webpack: ERROR in Cannot find module "./_baseClone", what am I missing?