I'm trying to make a single js bundle file using babel & webpack, and I'm constantly getting these parse errors, as if appropriate babel transforms are not working at all. I'm using 'react-native' preset for babel, which includes following plugins:
'babel-plugin-react-transform'
'babel-plugin-syntax-async-functions'
'babel-plugin-syntax-class-properties'
'babel-plugin-syntax-trailing-function-commas'
'babel-plugin-transform-class-properties'
'babel-plugin-transform-es2015-arrow-functions'
'babel-plugin-transform-es2015-block-scoping'
'babel-plugin-transform-es2015-classes'
'babel-plugin-transform-es2015-computed-properties'
'babel-plugin-transform-es2015-constants'
'babel-plugin-transform-es2015-destructuring'
'babel-plugin-transform-es2015-modules-commonjs'
'babel-plugin-transform-es2015-parameters'
'babel-plugin-transform-es2015-shorthand-properties'
'babel-plugin-transform-es2015-spread'
'babel-plugin-transform-es2015-template-literals'
'babel-plugin-transform-flow-strip-types'
'babel-plugin-transform-object-assign'
'babel-plugin-transform-object-rest-spread'
'babel-plugin-transform-react-display-name'
'babel-plugin-transform-react-jsx'
'babel-plugin-transform-regenerator'
'babel-plugin-transform-es2015-for-of'
So this plugin 'babel-plugin-syntax-class-properties' should do the transform I'm looking for but for some reason it's not working. Even when I add this plugin manually to .babelrc file I get the same output. This is the error I'm getting:
ERROR in ./~/#exponent/react-native-navigator/ExNavigator.js
Module parse failed: /Users/Arbor/Desktop/ArborMobileApp/backend/node_modules/#exponent/react-native-navigator/ExNavigator.js Unexpected token (24:16)
You may need an appropriate loader to handle this file type.
|
| export default class ExNavigator extends React.Component {
| static Styles = ExNavigatorStyles;
| static SceneConfigs = ExSceneConfigs;
| static Icons = ExNavigatorIcons;
# ./logic/initialize.js 14:175-218
Here is my .babelrc file:
{
"presets": ["react-native"],
}
My webpack.config.js:
const webpack = require('webpack');
module.exports = {
entry: './entryPoint.js',
output: {
path: './release',
filename: 'basecomponents.js'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
}
};
And package.json:
{
"name": "react-native-base-components",
"private": true,
"version": "0.0.1",
"description": "",
"main": "entryPoint",
"dependencies": {
"#exponent/react-native-navigator": "^0.4.2",
"react-native-orientation": "^1.15.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-loader": "^6.2.3",
"webpack": "^2.0.7-beta"
},
"scripts": {
"build": "rm -f dist/basecomponents.js && clear && node_modules/.bin/webpack --progress"
}
}
What am I doing wrong? Thanks.
Related
I'm trying to write a NPM package with some React stuff included, at the moment it's just a component and a hook. To build the package I'm using Webpack. I've added react and react-dom to the externals section to ensure that it's not included in the bundle. I've also marked react as a peerDependency in the package.json and included it as a devDependency. Still I'm getting the error Invalid hook call when trying to use the bundle in another project. I think I've tried everything that I can Google my way to (like using the package with the purpose to solve this) with no luck.
My Webpack config looks like this at the moment:
const path = require('path');
const isProduction = process.env.NODE_ENV?.toLowerCase() === 'production';
const config = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
libraryTarget: 'umd',
},
plugins: [],
module: {
rules: [
{
test: /\.(tsx?)$/i,
loader: 'ts-loader',
exclude: ['/node_modules/'],
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.graphql', '.json'],
},
externals: {
react: 'react',
'react-dom': 'react-dom',
},
};
module.exports = () => {
if (isProduction) {
config.mode = 'production';
} else {
config.mode = 'development';
}
return config;
};
The essentials from package.json looks like this:
{
...
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
...
"scripts": {
...
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
...
"prepublishOnly": "npm run build"
},
...
"peerDependencies": {
"react": "16.8.0 || ^17.0.2"
},
"peerDependenciesMeta": {
"react": {
"optional": true
}
},
"dependencies": {
"#apollo/client": "^3.3.19",
"apollo-server-errors": "^2.5.0",
"joi": "^17.4.0",
"lodash": "^4.17.21"
},
"devDependencies": {
...
"react": "^17.0.2",
"react-dom": "^17.0.2",
...
}
}
The hook is very simple, it's just trying to a context created by the component using useContext, to be sure that there isn't an issue with this logic I've tried to just use setState with the same result. The hook looks somewhat like this:
function useClient(): Client {
return useContext(getContext());
}
getContext is just a function which either creates or re-uses a existing React.Context (heavily inspired by Apollo Client):
const cache = new (canUseWeakMap ? WeakMap : Map)<
typeof createContext,
Context<Client | undefined>
>();
function getContext() {
let context = cache.get(createContext);
if (!context) {
context = createContext<Client | undefined>(undefined);
context.displayName = 'ClientContext';
cache.set(createContext, context);
}
return context;
}
export default getContext;
The component where I'm trying to use the hook is a simple functional component:
const HelloWorld: FC<HelloWorldProps> = () => {
const client = useClient();
return (
<div>Hello World!</div>
);
};
What am I missing? Really appreciate the help!
EDIT:
I reproduced the issue in a small sample app with just the basics, a external package using setState and then using that package in a create-react-app with the same result:
https://github.com/ganhammar/invalid-hook-call
Thanks for all the help!
The issue was that I stopped publishing packages and instead installed the dependency locally using file:../Client. That caused duplicate instances of React since it used the local-to-the-Client-package instance of React. Publishing only the built output and then installing that dependency solved the issue for me.
I found the following answer helpful for me to realize this (linking the react dependency between the two packages) if anyone else stumbles upon this.
I'm writing a React component library which I want to use in other projects without much overhead ( bit, create-react-library, generact, etc. ) and without publishing. I want to use npm install ../shared_lib to add it to my project as a symlink in /node_modules. This command adds the symlink to project node_modules. In my shared_lib I just have a test to export default a <div></div>:
import React from 'react';
const TryTest = function() {
return (
<div>
TryTest
</div>
)
}
export default TryTest;
The problem I'm facing is the following error when I import the component into my working project:
import TryTest from 'shared_lib';
Error:
ERROR in ../shared_lib/src/index.js 6:4
Module parse failed: Unexpected token (6:4)
You may need an appropriate loader to handle this file type.
| const TryTest = function() {
| return (
> <div>
| TryTest
| </div>
# ./src/App.js 27:0-33 28:12-19
# ./src/index.js
# multi babel-polyfill ./src/index.js
If I import anything from shared_lib other than a file with jsx - for example, a string or a function, etc. - it works fine.
EDIT: the application webpack has resolve object's symlinks prop set to false:
resolve: {
symlinks: false
},
EDIT: After applying the solution in the answer below (https://stackoverflow.com/a/60980492/3006493), I later changed symlinks prop back to true. I didn't need to set it to false for the solution to work and render shared_lib components.
My app's loader:
{
test: /\.jsx?$/,
include: [
path.join( __dirname, 'src'), // app/src
fs.realpathSync(__dirname + '/node_modules/shared_lib'), // app/node_modules/shared_lib/dist/shared_lib.js
],
exclude: /node_modules/,
use: [ 'babel-loader' ]
}
EDIT: When I applied the solution in the answer below, the loader now looks like this:
{
test: /\.jsx?$/,
include: [
path.join( __dirname, 'src'), // app/src
fs.realpathSync(__dirname + '/node_modules/shared_lib'), // app/node_modules/shared_lib/dist/shared_lib.js
],
exclude: /node_modules/,
use: [ {
loader: 'babel-loader',
options: require("./package.json").babel
}
]
}
App's current .babelrc settings (I also tried removing .babelrc and including the presets in package.json with same result):
{
"presets": [ "#babel/preset-react", "#babel/preset-env"]
}
**EDIT: After applying the solution in the answer below, I ended up putting babel presets back into package.json.
"babel": {
"presets": [
"#babel/preset-react",
"#babel/preset-env"
]
},
I researched for a while to find a solution to this and apparently webpack has issues bundling symlinked react components? I am not using create-react-app.
So, I tried to bundle the shared_lib before importing it into the project, just to see what would happen. Here's the final webpack config (I tried other configurations as well):
const pkg = require('./package.json');
const path = require('path');
const buildPath = path.join( __dirname, 'dist' );
const clientPath = path.join( __dirname, 'src');
const depsPath = path.join( __dirname, 'node_modules');
const libraryName = pkg.name;
module.exports = [
'cheap-module-source-map'
].map( devtool => ({
bail: true,
mode: 'development',
entry: {
lib : [ 'babel-polyfill', path.join( clientPath, 'index.js' ) ]
},
output: {
path: buildPath,
filename: 'shared_lib.js',
libraryTarget: 'umd',
publicPath: '/dist/',
library: libraryName,
umdNamedDefine: true
},
// to avoid bundling react
externals: {
'react': {
commonjs: 'react',
commonjs2: 'react',
amd: 'React',
root: 'React'
}
},
module: {
rules: [
{
test: /\.jsx?$/,
include: [
clientPath
],
exclude: /node_modules/,
use: [ 'babel-loader' ],
},
]
},
devtool,
optimization: {
splitChunks: {
chunks: 'all',
},
}
}));
And the package.json for the shared_lib
{
"name": "shared_lib",
"version": "1.0.0",
"description": "",
"main": "dist/shared_lib.js",
"scripts": {
"clean": "rm -rf dist/",
"build": "$(npm bin)/webpack --config ./webpack.config.js",
"prepublish": "npm run clean && npm run build"
},
"author": "",
"license": "ISC",
"peerDependencies": {
"react": "^16.8.6"
},
"devDependencies": {
"react": "^16.8.6",
"#babel/core": "^7.9.0",
"#babel/preset-env": "^7.9.0",
"#babel/preset-react": "^7.9.4",
"babel-loader": "^8.1.0",
"babel-polyfill": "^6.26.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11"
},
"babel": {
"presets": [
"#babel/preset-react",
"#babel/preset-env"
]
}
}
The package is bundled without errors:
When I try to import the component in the same way:
import TryTest from 'shared_lib';
The console.log returns undefined.
The path to the library file in my app is fine, because if I erase everything in shared_lib/dist/shared_lib.js and just write export default 1 the console.log(TryTest) in my App.js will return 1.
I tried changing libraryTarget property in shared_lib/webpack.config to libraryTarget: 'commonjs'. The result of console.log(TryTest) becomes {shared_lib: undefined}.
Has anyone ever run into this?
I found what finally worked for me and rendered the symlinked shared_lib to the app.
This answer: https://github.com/webpack/webpack/issues/1643#issuecomment-552767686
Worked well rendering symlinked shared_lib components. I haven't discovered any drawbacks from using this solution, but it's the only one that worked so far.
I'm trying to have my scss run through prettier but always get errors. I'm using the prettier-webpack-loader (also tried with prettier-webpack-plugin). When I run it, it seems to want to process the .scss files as javascript. One of my js files looks like this:
require("./header.component.scss");
angular
.module("app")
...
Header file looks like this:
header{
...
}
I have the following loader for webpack:
...{
"test": /\.scss$/,
"use": ExtractTextPlugin.extract({
"use": ["css-loader", "sass-loader"]
})
}, {
"test": /\.(js|scss)$/,
"include": path.resolve(__dirname, "website/src"),
"loader": "prettier-webpack-loader",
"options": {
"useTabs": true
}
}
...
When I run it I get the following error:
ERROR in ./website/src/components/header.component.scss
Module build failed: ModuleBuildError: Module build failed: SyntaxError: Unexpected token, expected ; (1:7)
> 1 | header{
| ^
2 | nav.bg-primary{
3 | padding:0.7rem 1rem;
4 | z-index: 10000;
at createError$1 (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\prettier\parser-babylon.js:1:112)
at parse (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\prettier\parser-babylon.js:1:783)
at Object.parse (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\prettier\index.js:3785:12)
at formatWithCursor (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\prettier\index.js:21730:22)
at format (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\prettier\index.js:21770:10)
at Object.format (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\prettier\index.js:21995:12)
at Object.module.exports (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\prettier-webpack-loader\index.js:11:35)
at runLoaders (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\webpack\lib\NormalModule.js:195:19)
at C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\loader-runner\lib\LoaderRunner.js:364:11
at C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\loader-runner\lib\LoaderRunner.js:230:18
at runSyncOrAsync (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\loader-runner\lib\LoaderRunner.js:143:3)
at iterateNormalLoaders (C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\loader-runner\lib\LoaderRunner.js:229:2)
at C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\loader-runner\lib\LoaderRunner.js:202:4
at C:\Development\BPI.InContext\src\Bpi.InContext\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:70:14
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
If I run prettier through the CLI it works fine.
It looks like prettier-webpack-loader does not pass the file path to Prettier, so there's no way for it to know it should parse as a SCSS file.
I think if you separate the loaders and add the parser to the options it might work:
{
"test": /\.js$/,
"include": path.resolve(__dirname, "website/src"),
"loader": "prettier-webpack-loader",
"options": {
"useTabs": true
}
},
{
"test": /\.scss$/,
"include": path.resolve(__dirname, "website/src"),
"loader": "prettier-webpack-loader",
"options": {
"parser": "postcss",
"useTabs": true
}
}
How can I create a webpack library ES6, that can be consumed in ReactJS. Like
-----lib.js-----
export function sum(a , b) {
return a + b;
};
export function multiply(a, b) {
return a * b;
};
I want to consume using import statements like:-
import lib from './lib';
console.log(lib.sum(3,5));
I am using following as webpack.config.babel.js
export default () => (
{
entry: './src/lib.js',
output: {
path: './dist',
filename: 'lib.js',
libraryTarget: 'umd',
library: 'lib'
},
module: {
rules: [
{test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
]
},
}
);
.babelrc file contains following configuration
{
"presets": ["es2015"]
}
package.json contents
{
"name": "lib",
"version": "1.0.0",
"description": "",
"main": "dist/lib.js",
"scripts": {
"build:lib": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.0",
"babel-preset-es2015-webpack": "^6.4.3",
"webpack": "^2.2.1"
}
}
Everything is proper other than the way you are importing, use this to import all the function defined in this file:
import * as lib from './lib';
Now you can use any function by lib.sum(1,2);
Reason: you are exporting many functions so you need to import each function separately or use * to import all.
Alternate way:
use module.exports to export many methods, lib.js file:
module.exports = {
sum: function(a,b){
return a + b;
},
multiply: function(a,b){
return a * b;
}
}
import this file by:
import Lib from "lib";
Now use it by lib.sum(1,2);
Reference: https://danmartensen.svbtle.com/build-better-apps-with-es6-modules
Couple of things pop out:
If you export everything, you should also import everything:
import * as lib from './lib';
(as stated in a deleted answer by #MayankShukla)
output.path must be absolute:
output: {
path: path.resolve(__dirname, 'dist'),
// ...
I am trying to install gun.js and run it inside a Reactjs webpack bundled app
var path = require('path'),
webpack = require('webpack');
module.exports = {
devtool: 'source-map',
target: 'node',
node: {
fs: 'empty'
},
entry: {
workboard: './src/workboard/main.js'
},
output: {
path: __dirname, filename: '/public/[name]/js/bundle.js'
},
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', 'stage-2', 'stage-1']
}
}
],
noParse: [/aws-sdk/]
},
plugins: [
new webpack.DefinePlugin({ "global.GENTLY": false })
]
};
package.json looks like this
{
"name": "workbench",
"version": "1.0.0",
"description": "My local workbench",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cd public && serve"
},
"author": "kn#unisport.dk",
"license": "ISC",
"dependencies": {
"babel-core": "^6.7.7",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"fetch": "^1.0.1",
"react": "^0.14.8",
"react-dom": "^0.14.8",
"react-router": "^2.3.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"bufferutil": "^1.2.1",
"gun": "^0.3.992",
"loader-utils": "^0.2.15",
"url": "^0.11.0",
"utf-8-validate": "^1.2.1",
"webpack": "^2.1.0-beta.5"
}
}
js test code in main.js looks like this
/**
* Main.js
*/
'use strict';
/**
* Setup Gun
* TODO: add peers
*/
var Gun = require('gun');
var gun = Gun();
var React = require('react');
var ReactDom = require('react-dom');
var App = React.createClass({
render() {
return <div>Hello</div>
}
});
var ROOT = document.getElementById('appmount');
ReactDom.render(
<App />,
ROOT
);
but when I load index.html that includes bundle.js I get this error in the console
Uncaught ReferenceError: require is not defined
module.exports = require("url");
/*****************
** WEBPACK FOOTER
** external "url"
** module id = 21
** module chunks = 0
**/
what is it that I'm missing?
Update
Changing node to 'web' as suggested, but this gives me
ERROR in ./~/ws/lib/WebSocketServer.js
Module not found: Error: Can't resolve 'tls' in '/Users/kn/Documents/workbench/node_modules/ws/lib'
# ./~/ws/lib/WebSocketServer.js 15:10-24
ERROR in ./~/diffie-hellman/lib/primes.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/diffie-hellman/lib/primes.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.
| {
| "modp1": {
| "gen": "02",
| "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
# ./~/diffie-hellman/browser.js 2:13-41
ERROR in ./~/elliptic/package.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/elliptic/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
| {
| "_args": [
| [
| {
# ./~/elliptic/lib/elliptic.js 5:19-45
ERROR in ./~/parse-asn1/aesid.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/parse-asn1/aesid.json Unexpected token (1:25)
You may need an appropriate loader to handle this file type.
| {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
| "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
| "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
# ./~/parse-asn1/index.js 2:12-35
Installing tls results in this error
ERROR in ./~/diffie-hellman/lib/primes.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/diffie-hellman/lib/primes.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.
| {
| "modp1": {
| "gen": "02",
| "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
# ./~/diffie-hellman/browser.js 2:13-41
ERROR in ./~/elliptic/package.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/elliptic/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
| {
| "_args": [
| [
| {
# ./~/elliptic/lib/elliptic.js 5:19-45
ERROR in ./~/parse-asn1/aesid.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/parse-asn1/aesid.json Unexpected token (1:25)
You may need an appropriate loader to handle this file type.
| {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
| "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
| "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
# ./~/parse-asn1/index.js 2:12-35
I tried to install primes, but Im getting
ERROR in ./~/diffie-hellman/lib/primes.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/diffie-hellman/lib/primes.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.
| {
| "modp1": {
| "gen": "02",
| "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
# ./~/diffie-hellman/browser.js 2:13-41
ERROR in ./~/elliptic/package.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/elliptic/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
| {
| "_args": [
| [
| {
# ./~/elliptic/lib/elliptic.js 5:19-45
ERROR in ./~/parse-asn1/aesid.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/parse-asn1/aesid.json Unexpected token (1:25)
You may need an appropriate loader to handle this file type.
| {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
| "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
| "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
# ./~/parse-asn1/index.js 2:12-35
Updating once again after changing the code inside main.js
Suggestion from #marknadal did the trick
main.js
/**
* Main.js
*/
'use strict';
/**
* Setup Gun
* TODO: add peers
*/
var Gun = require('gun/gun');
var peers = [
'http://localhost:8080/gun'
];
var gun = Gun(peers);
var React = require('react');
var ReactDom = require('react-dom');
var App = React.createClass({
render() {
return <div>Hello</div>
}
});
var ROOT = document.getElementById('appmount');
ReactDom.render(
<App />,
ROOT
);
And webpack.config
var path = require('path'),
webpack = require('webpack');
module.exports = {
devtool: 'source-map',
target: 'web',
node: {
fs: 'empty'
},
entry: {
workboard: './src/workboard/main.js'
},
output: {
path: __dirname, filename: '/public/[name]/js/bundle.js'
},
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', 'stage-2', 'stage-1']
}
},
{
test: /\.json$/,
loader: 'json',
include: [
/node_modules/
]
}
],
noParse: [/aws-sdk/]
},
plugins: [
new webpack.DefinePlugin({ "global.GENTLY": false })
]
};
and package.json - it does include a lot more than what's needed for this project, disregard that if you want to attempt to get this running on your own
{
"name": "workbench",
"version": "1.0.0",
"description": "My local workbench",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cd public && serve"
},
"author": "kn#unisport.dk",
"license": "ISC",
"dependencies": {
"babel-core": "^6.7.7",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"express": "^4.14.0",
"fetch": "^1.0.1",
"react": "^0.14.8",
"react-dom": "^0.14.8",
"react-router": "^2.3.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"bufferutil": "^1.2.1",
"gun": "^0.3.992",
"json-loader": "^0.5.4",
"loader-utils": "^0.2.15",
"prime": "^0.5.0",
"primes": "0.0.1",
"tls": "0.0.1",
"url": "^0.11.0",
"utf-8-validate": "^1.2.1",
"webpack": "^2.1.0-beta.5"
}
}
Now when I use webpack --watch no warnings or errors are shown. Going to public/workboad and running serve, I see the react application running with no errors
Did #riscarrott 's answer work? I'm the author of gun, and it looks like 1 of the errors is gun related. However I am not a webpack expert so I am unsure what is the problem.
I do know that require('gun') actually loads ./index.js that in turn loads server-side specific logic (which won't work in the browser). If riscarrott 's answer does not work, try replacing require('gun') with require('gun/gun') and see if it works. If this is the case, please file a bug report on https://github.com/amark/gun so we can get this fixed for future people.
If this did not help, several other people on the team and the community use webpack and gun a lot. I'll see if I can get them to reply here.
EDIT: It looks like the de facto way of other projects, like jquery/angular/etc. (https://www.npmjs.com/package/angular) is to have you include them with a < script > tag. Therefore we also recommend you do this as well, as it avoids all these build problems.
<script src="/node_modules/gun/gun.js"></script>
It looks like you're running your code in the browser but you're targeting 'node' so Webpack will leave require statements untouched when referencing builtin modules such as 'url'.
To fix this remove target: 'node'.
My first instinct is maybe you can add a variable that can be detected at build time to maybe overcome the issues...
on https://github.com/petehunt/webpack-howto section 6 (I know there's ways to define like 'ws' to not be pulled, because that will be provided by the browser target; I just don't see it on that page)
On my own project gun was failing to browserify, because of the optional require( 'ws' ) and other things, so I excluded it from packing, and just serve it directly. I also pulled require.js so I could require('gun') at a javascript level, just outside of the package and more in the application of the library-package.
Could also just fall back further to pulling gun using a script tag...