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...
Related
I have a Symfony project, where the frontend-pipeline is built with Webpack Encore, I would like to add React to the project and have a React widget inserted into one of my server-side rendered pages.
I've followed the documentation and extended my webpack.config.js with the following one-liner .enableReactPreset(). I'm also writting my JS in TypeScript (I already have some non-react based JS in the project that's compiled from TS, thus I'm writing my react components in .tsx files).
I create an entrypoint for my react widget that looks like this
import React from "react";
import ReactDOM from "react-dom";
import "./map.scss";
import App from "./components/App";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("map-app")
);
When I start the app in watch mode with encore dev I don't see any errors in my console and the code compiles, but when I check the output in my browser I get an error, telling me that it can not read property render of undefined.
I've logged out React and ReactDOM and they both show to be undefined.
The strange thing is that my editor (PHPStrom) sees the imported module pointing to the respective index.d.ts files of these packages and intellisense works on them (I get the render method autocompleted for instance).
I've enabled "allowSyntheticDefaultImports": true in my project's tsconfig.json.
Now I could solve the problem with a workaround. If I import these libraries like so:
import * as React from "react";
import * as ReactDOM from "react-dom";
console.log(React); // no longer undefined, I get an object with all of react's methods
It works, but it's a bit awkward, and also this way I can't import the named components from react.
For instance this also fails:
import {useState} from "react";
console.log(useState); // undefined
I can only make it work like so:
import * as React from "react";
console.log(React.useState); // I get the function
I would like to understand why is this happening and how to make the default exports work properly like in all other React projects.
My package.json currently looks like this:
{
"devDependencies": {
"#babel/preset-react": "^7.16.7",
"#symfony/webpack-encore": "^1.7.*",
"#types/jquery": "^3.3.33",
"#types/leaflet": "^1.5.5",
"#types/leaflet.markercluster": "^1.4.2",
"#types/react": "^17.0.38",
"#types/react-dom": "^17.0.11",
"#types/react-router-dom": "^5.3.2",
"autoprefixer": "^9.6.1",
"core-js": "^3.0.0",
"file-loader": "^6.2.0",
"fork-ts-checker-webpack-plugin": "^5.0.0",
"postcss": "^8.4.5",
"postcss-loader": "^6.0.0",
"sass": "^1.3.0",
"sass-loader": "^12.0.0",
"ts-loader": "^9.0.0",
"typescript": "^3.6.4",
"webpack-notifier": "^1.6.0"
},
"repository": {
"type": "git",
"url": "https://github.com/Cooty/the-bedechka-case"
},
"name": "the-bedechka-case-website-frontend",
"license": "MIT",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
},
"browserslist": [
"defaults"
],
"dependencies": {
"bootstrap": "^4.4.1",
"fg-loadcss": "^2.1.0",
"html5sortable": "^0.10.0",
"jquery": "3.5",
"leaflet": "^1.7.1",
"leaflet.markercluster": "^1.4.1",
"popper.js": "^1.16.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-leaflet": "^3.2.2",
"react-router-dom": "^6.2.1"
}
}
This is my current tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "react",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"noEmitOnError": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
},
"exclude": [
"vendor",
"var",
"translations",
"tests",
"src",
"public",
"node_modules"
]
}
and my webpack.config.js
const Encore = require("#symfony/webpack-encore");
const babelLoader = {
test: /\.js$/,
loader: "babel-loader",
options: {
presets: [
[
"#babel/preset-env",
{
"useBuiltIns": "entry",
"corejs": {version: 3, proposals: true}
},
]
]
}
};
Encore
.setOutputPath("public/build/")
.copyFiles({
from: "./assets/images",
to: "images/[path][name].[hash:8].[ext]"
})
.setPublicPath("/build")
.cleanupOutputBeforeBuild()
.enableVersioning(Encore.isProduction())
.enableSourceMaps()
.enableReactPreset()
.addEntry("app", "./templates/ts/app.ts")
.addEntry("home", "./templates/home/home.ts")
.addEntry("map", "./templates/map/map.tsx")
.addEntry("yt", "./templates/components/youtube-embed/youtube-embed.ts")
.addEntry("admin", "./templates/admin/ts/admin.ts")
.addStyleEntry("the-crew", "./templates/the-crew/the-crew.scss")
.addStyleEntry("protagonists", "./templates/protagonists/protagonists.scss")
.addStyleEntry("screenings", "./templates/screenings/screenings.scss")
.addStyleEntry("partners", "./templates/partners/partners.scss")
.addStyleEntry("critical-path", "./templates/scss/critical-path.scss")
.addStyleEntry("critical-path-home", "./templates/home/critical-path-home.scss")
.addStyleEntry("critical-path-subpages", "./templates/scss/critical-path-subpages.scss")
.enableTypeScriptLoader()
.enableForkedTypeScriptTypesChecking()
.addLoader(babelLoader)
.enablePostCssLoader()
.enableSassLoader()
.enableSingleRuntimeChunk()
.splitEntryChunks();
const config = Encore.getWebpackConfig();
config.optimization.noEmitOnErrors = true;
module.exports = config;
You can also checkout the branch where this feature is being implemented here.
In our case we have in compiler options "esModuleInterop": true, so we have something like this:
{
"ecmaVersion": 2021,
"sourceType": "module",
"compilerOptions": {
"noImplicitAny": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"jsx": "react"
},
"include": [
"assets/js/**/*.ts",
"assets/js/**/*.tsx",
"assets/js/**/*.js",
"assets/js/**/*.jsx",
"templates/**/*.ts",
"templates/**/*.tsx",
"templates/**/*.js",
"templates/**/*.jsx"
],
"exclude": [
"node_modules",
"public",
"vendor"
]
}
We have a more complex webpackconfig be we use the same options regarding React:
const Encore = require('#symfony/webpack-encore');
const path = require('path');
const ESLintPlugin = require('eslint-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const {
bundlesScssPath,
bundlesJsPath,
templatesPath
} = require('./webpack/configs/utils.config');
const vendorsAddEntries = require('./webpack/configs/vendors-add-entries.config');
const projectAddEntries = require('./webpack/configs/project-add-entries.config');
const coreVendorsAddEntries = require('./webpack/configs/core-vendors-add-entries.config');
const designStoryAddEntries = require('./webpack/configs/design-story-add-entries.config');
const cssAddEntries = require('./webpack/configs/css-add-entries.config');
vendorsAddEntries();
coreVendorsAddEntries();
projectAddEntries();
designStoryAddEntries();
cssAddEntries();
// +-------------+
// | Main config |
// +-------------+
// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
Encore
// directory where compiled assets will be stored
.setOutputPath('public/build/')
// public path used by the web server to access the output path
.setPublicPath('/build')
// only needed for CDN's or sub-directory deploy
//.setManifestKeyPrefix('build/')
/*
* ENTRY CONFIG
*
* Add 1 entry for each "page" of your app
* (including one that's included on every page - e.g. "app")
*
* Each entry will result in one JavaScript file (e.g. app.js)
* and one CSS file (e.g. app.scss) if you JavaScript imports CSS.
*/
// +-------------------+
// | common js entries |
// +-------------------+
// .addEntry('app', './assets/js/app.ts')
.addEntry('vendors', path.resolve('./' + path.join(bundlesJsPath, 'vendors/index.ts')))
.addEntry('components', path.resolve('./' + path.join(templatesPath, 'DesignStory/components/components.ts')))
// +---------------+
// | configuration |
// +---------------+
/*
* FEATURE CONFIG
*
* Enable & configure other features below. For a full
* list of features, see:
* https://symfony.com/doc/current/frontend.html#adding-more-features
*/
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css) and assets cache issues on production
.enableVersioning(Encore.isProduction())
// When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
.splitEntryChunks()
// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
.enableSingleRuntimeChunk()
// enables #babel/preset-env polyfills
.configureBabel(() => {}, {
useBuiltIns: 'usage',
corejs: 3
})
// +------------+
// | TS + React |
// +------------+
// uncomment if you use API Platform Admin (composer req api-admin)
.enableReactPreset()
.enableTypeScriptLoader((tsConfig) => {
// You can use this callback function to adjust ts-loader settings
// https://github.com/TypeStrong/ts-loader/blob/master/README.md#loader-options
// For example:
// tsConfig.silent = false
})
// optionally enable forked type script for faster builds
// https://www.npmjs.com/package/fork-ts-checker-webpack-plugin
// requires that you have a tsconfig.json file that is setup correctly.
.enableForkedTypeScriptTypesChecking(options => {
delete options.parser;
})
.addPlugin(new ESLintPlugin({
extensions: ['js', 'jsx', 'ts', 'tsx'],
exclude: [
'node_modules',
'vendor'
],
}))
.addPlugin(new StyleLintPlugin({
context: path.resolve(__dirname),
configFile: '.stylelintrc.json',
files: [
path.join(bundlesScssPath, '**/*.s+(a|c)ss'),
path.join(templatesPath, '**/*.s+(a|c)ss')
]
}))
// +-----------------+
// | SASS/postprefix |
// +-----------------+
// enables Sass/SCSS support
.enableSassLoader((options) => {
// add or set custom options
// options.includePaths: ["absolute/path/a", "absolute/path/b"];
// options.warnRuleAsWarning = true;
}, {}
)
// Convert fonts to base64 inline (avoid font loading issue on pdf to html generation with wkhtml2pdf)
.addLoader({
test: /\.(ttf|eot|woff(2)?)(\?[a-z0-9=&.]+)?$/,
use: ['base64-inline-loader'],
type: 'javascript/auto',
})
// enable autoprefixing for css
.enablePostCssLoader((options) => {
options.postcssOptions = {
// the directory where the postcss.config.js file is stored
config: path.resolve(__dirname, 'postcss.config.js')
};
})
// uncomment to get integrity="..." attributes on your script & link tags
// requires WebpackEncoreBundle 1.4 or higher
//.enableIntegrityHashes()
.addExternals(
{
'bazinga-translator': 'Translator'
}
)
// uncomment if you're having problems with a jQuery plugin
.autoProvidejQuery()
// you can use this method to provide other common global variables,
// such as '_' for the 'underscore' library
// .autoProvideVariables({
// $: 'jquery',
// jQuery: 'jquery',
// 'window.jQuery': 'jquery',
// })
.copyFiles({
from: './assets/images',
to: 'images/[path][name].[ext]',
pattern: /\.(png|jpg|jpeg|svg|ico|gif)$/
})
// +-------------+
// | performance |
// +-------------+
// A simple technique to improve the performance of web applications is to reduce
// the number of HTTP requests inlining small files as base64 encoded URLs in the
// generated CSS files.
.configureImageRule({
// tell Webpack it should consider inlining
type: 'asset',
//maxSize: 4 * 1024, // 4 kb - the default is 8kb
})
// same here
.configureFontRule({
type: 'asset',
//maxSize: 4 * 1024
})
;
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
module.exports = Encore.getWebpackConfig();
Hopes it will help
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 have a React application configured with Django Application. I have different webpack config files for development and production, When I run "npm run start", it gives me error that:
Cannot read property 'tap' of undefined
package.json file:
{
"name": "project",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/core": "7.1.0",
"#svgr/webpack": "2.4.1",
"ajv": "^6.5.4",
"ajv-keywords": "^3.2.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "23.6.0",
"babel-loader": "8.0.4",
"babel-plugin-named-asset-import": "^0.2.2",
"babel-preset-react-app": "^5.0.4",
"bfj": "6.1.1",
"case-sensitive-paths-webpack-plugin": "2.1.2",
"chalk": "2.4.1",
"css-loader": "1.0.0",
"dotenv": "6.0.0",
"dotenv-expand": "4.2.0",
"eslint": "5.6.0",
"eslint-config-react-app": "^3.0.4",
"eslint-loader": "2.1.1",
"eslint-plugin-flowtype": "2.50.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-jsx-a11y": "6.1.2",
"eslint-plugin-react": "7.11.1",
"file-loader": "2.0.0",
"fs-extra": "7.0.0",
"identity-obj-proxy": "3.0.0",
"jest": "23.6.0",
"jest-pnp-resolver": "1.0.1",
"jest-resolve": "23.6.0",
"mini-css-extract-plugin": "0.4.3",
"optimize-css-assets-webpack-plugin": "5.0.1",
"pnp-webpack-plugin": "1.1.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-preset-env": "6.0.6",
"postcss-safe-parser": "4.0.1",
"react": "^16.6.0",
"react-app-polyfill": "^0.1.3",
"react-dev-utils": "^6.0.5",
"react-dom": "^16.6.0",
"resolve": "1.8.1",
"sass-loader": "7.1.0",
"style-loader": "0.23.0",
"terser-webpack-plugin": "1.1.0",
"url-loader": "1.1.1",
"webpack": "4.19.1",
"webpack-dev-server": "3.1.9",
"webpack-manifest-plugin": "2.0.4",
"workbox-webpack-plugin": "3.6.2"
},
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx}"
],
"resolver": "jest-pnp-resolver",
"setupFiles": [
"react-app-polyfill/jsdom"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx}",
"<rootDir>/src/**/?(*.)(spec|test).{js,jsx}"
],
"testEnvironment": "jsdom",
"testURL": "http://localhost",
"transform": {
"^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"json",
"web.jsx",
"jsx",
"node"
]
},
"babel": {
"presets": [
"react-app"
]
},
"devDependencies": {
"html-webpack-plugin": "^4.0.0-beta.2",
"react-hot-loader": "^4.0.0",
"tap": "^11.0.0-rc.3",
"webpack-bundle-tracker": "^0.4.2-beta",
"webpack-cli": "^3.1.2"
}
}
webpack.config.dev.js file:
"use strict";
const path = require("path");
const webpack = require("webpack");
const PnpWebpackPlugin = require("pnp-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CaseSensitivePathsPlugin = require("case-sensitive-paths-
webpack-plugin");
const InterpolateHtmlPlugin = require("react-dev-
utils/InterpolateHtmlPlugin");
const WatchMissingNodeModulesPlugin = require("react-dev-
utils/WatchMissingNodeModulesPlugin");
const ModuleScopePlugin = require("react-dev-
utils/ModuleScopePlugin");
const getCSSModuleLocalIdent = require("react-dev-
utils/getCSSModuleLocalIdent");
const getClientEnvironment = require("./env");
const paths = require("./paths");
const ManifestPlugin = require("webpack-manifest-plugin");
const ModuleNotFoundPlugin = require("react-dev-
utils/ModuleNotFoundPlugin");
// Webpack uses `publicPath` to determine where the app is being
served from.
// In development, we always serve from the root. This makes config
easier.
//const publicPath = '/';
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in
JavaScript.
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than
%PUBLIC_PATH%xyz.
const publicPath = "http://localhost:3000/";
const publicUrl = "http://localhost:3000/";
// Get environment variables to inject into our app.
const env = getClientEnvironment(publicUrl);
const BundleTracker = require("webpack-bundle-tracker");
// style files regexes
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
// common function to get style loaders
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
require.resolve("style-loader"),
{
loader: require.resolve("css-loader"),
options: cssOptions
},
{
// Options for PostCSS as we reference these options twice
// Adds vendor prefixing based on your specified browser support in
// package.json
loader: require.resolve("postcss-loader"),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebook/create-react-app/issues/2677
ident: "postcss",
plugins: () => [
require("postcss-flexbugs-fixes"),
require("postcss-preset-env")({
autoprefixer: {
flexbox: "no-2009"
},
stage: 3
})
]
}
}
];
if (preProcessor) {
loaders.push(require.resolve(preProcessor));
}
return loaders;
};
// This is the development configuration.
// It is focused on developer experience and fast rebuilds.
// The production configuration is different and lives in a separate
file.
module.exports = {
mode: "development",
// You may want 'eval' instead if you prefer to see the compiled output in DevTools.
// See the discussion in https://github.com/facebook/create-react-app/issues/343
devtool: "cheap-module-source-map",
// These are the "entry points" to our application.
// This means they will be the "root" imports that are included in JS bundle.
entry: [
// Include an alternative client for WebpackDevServer. A client's job is to
// connect to WebpackDevServer by a socket and get notified about changes.
// When you save a file, the client will either apply hot updates (in case
// of CSS changes), or refresh the page (in case of JS changes). When you
// make a syntax error, this client will display a syntax error overlay.
// Note: instead of the default WebpackDevServer client, we use a custom one
// to bring better experience for Create React App users. You can replace
// the line below with these two lines if you prefer the stock client:
// require.resolve('webpack-dev-server/client') + '?/',
// require.resolve('webpack/hot/dev-server'),
require.resolve("react-dev-utils/webpackHotDevClient"),
require.resolve("webpack-dev-server/client") + "?
http://localhost:3000",
require.resolve("webpack/hot/dev-server"),
// Finally, this is your app's code:
paths.appIndexJs
// We include the app code last so that if there is a runtime error during
// initialization, it doesn't blow up the WebpackDevServer client, and
// changing JS code would still trigger a refresh.
],
output: {
// Add /* filename */ comments to generated require()s in the output.
pathinfo: true,
// This does not produce a real file. It's just the virtual path that is
// served by WebpackDevServer in development. This is the JS bundle
// containing code from all our entry points, and the Webpack runtime.
filename: "static/js/bundle.js",
// There are also additional JS chunk files if you use code splitting.
chunkFilename: "static/js/[name].chunk.js",
// This is the URL that app is served from. We use "/" in development.
publicPath: publicPath,
// Point sourcemap entries to original disk location (format as URL on Windows)
devtoolModuleFilenameTemplate: info =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, "/")
},
optimization: {
// Automatically split vendor and commons
// https://twitter.com/wSokra/status/969633336732905474
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
splitChunks: {
chunks: "all",
name: false
},
minimize: false,
// Keep the runtime chunk seperated to enable long term caching
// https://twitter.com/wSokra/status/969679223278505985
runtimeChunk: true
},
resolve: {
// This allows you to set a fallback for where Webpack should look for modules.
// We placed these paths second because we want `node_modules` to "win"
// if there are any conflicts. This matches Node resolution mechanism.
// https://github.com/facebook/create-react-app/issues/253
modules: ["node_modules"].concat(
// It is guaranteed to exist because we tweak it in `env.js`
process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
),
// These are the reasonable defaults supported by the Node ecosystem.
// We also include JSX as a common component filename extension to support
// some tools, although we do not recommend using it, see:
// https://github.com/facebook/create-react-app/issues/290
// `web` extension prefixes have been added for better support
// for React Native Web.
extensions: [".mjs", ".web.js", ".js", ".json", ".web.jsx", ".jsx"],
alias: {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
"react-native": "react-native-web"
},
plugins: [
// Adds support for installing with Plug'n'Play, leading to faster installs and adding
// guards against forgotten dependencies and such.
PnpWebpackPlugin,
// Prevents users from importing files from outside of src/ (or node_modules/).
// This often causes confusion because we only process files within src/ with babel.
// To fix this, we prevent you from importing files out of src/ -- if you'd like to,
// please link the files into your node_modules/ and let module-resolution kick in.
// Make sure your source files are compiled, as they will not be processed in any way.
new BundleTracker({
path: paths.statsRoot,
filename: "webpack-stats.dev.json"
}),
new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson])
]
},
resolveLoader: {
plugins: [
// Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
// from the current package.
PnpWebpackPlugin.moduleLoader(module)
]
},
module: {
strictExportPresence: true,
rules: [
// Disable require.ensure as it's not a standard language feature.
{ parser: { requireEnsure: false } },
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|mjs|jsx)$/,
enforce: "pre",
use: [
{
options: {
formatter: require.resolve("react-dev-utils/eslintFormatter"),
eslintPath: require.resolve("eslint")
},
loader: require.resolve("eslint-loader")
}
],
include: paths.appSrc
},
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
// back to the "file" loader at the end of the loader list.
oneOf: [
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve("url-loader"),
options: {
limit: 10000,
name: "static/media/[name].[hash:8].[ext]"
}
},
// Process application JS with Babel.
// The preset includes JSX, Flow, and some ESnext features.
{
test: /\.(js|mjs|jsx)$/,
include: paths.appSrc,
loader: require.resolve("babel-loader"),
options: {
customize: require.resolve(
"babel-preset-react-app/webpack-overrides"
),
plugins: [
[
require.resolve("babel-plugin-named-asset-import"),
{
loaderMap: {
svg: {
ReactComponent: "#svgr/webpack?-prettier,-svgo![path]"
}
}
}
]
],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// Don't waste time on Gzipping the cache
cacheCompression: false
}
},
// Process any JS outside of the app with Babel.
// Unlike the application JS, we only compile the standard ES features.
{
test: /\.(js|mjs)$/,
exclude: /#babel(?:\/|\\{1,2})runtime/,
loader: require.resolve("babel-loader"),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [
[
require.resolve("babel-preset-react-app/dependencies"),
{ helpers: true }
]
],
cacheDirectory: true,
// Don't waste time on Gzipping the cache
cacheCompression: false,
// If an error happens in a package, it's possible to be
// because it was compiled. Thus, we don't want the browser
// debugger to show the original code. Instead, the code
// being evaluated would be much more helpful.
sourceMaps: false
}
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we use a plugin to extract that CSS to a file, but
// in development "style" loader enables hot editing of CSS.
// By default we support CSS Modules with the extension .module.css
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1
})
},
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
// using the extension .module.css
{
test: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
modules: true,
getLocalIdent: getCSSModuleLocalIdent
})
},
// Opt-in support for SASS (using .scss or .sass extensions).
// Chains the sass-loader with the css-loader and the style-loader
// to immediately apply all styles to the DOM.
// By default we support SASS Modules with the
// extensions .module.scss or .module.sass
{
test: sassRegex,
exclude: sassModuleRegex,
use: getStyleLoaders({ importLoaders: 2 }, "sass-loader")
},
// Adds support for CSS Modules, but using SASS
// using the extension .module.scss or .module.sass
{
test: sassModuleRegex,
use: getStyleLoaders(
{
importLoaders: 2,
modules: true,
getLocalIdent: getCSSModuleLocalIdent
},
"sass-loader"
)
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
// This loader doesn't use a "test" so it will catch all modules
// that fall through the other loaders.
{
// Exclude `js` files to keep "css" loader working as it injects
// its runtime that would otherwise be processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|mjs|jsx)$/, /\.html$/, /\.json$/],
loader: require.resolve("file-loader"),
options: {
name: "static/media/[name].[hash:8].[ext]"
}
}
]
}
// ** STOP ** Are you adding a new loader?
// Make sure to add the new loader(s) before the "file" loader.
]
},
plugins: [
// Generates an `index.html` file with the <script> injected.
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml
}),
// new InterpolateHtmlPlugin({
// name: "xu"
// }),
// Makes some environment variables available in index.html.
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
// In development, this will be an empty string.
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
// This gives some necessary context to module not found errors, such as
// the requesting resource.
new ModuleNotFoundPlugin(paths.appPath),
// Makes some environment variables available to the JS code, for example:
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
new webpack.DefinePlugin(env.stringified),
// This is necessary to emit hot updates (currently CSS only):
new webpack.HotModuleReplacementPlugin(),
// Watcher doesn't work well if you mistype casing in a path so we use
// a plugin that prints an error when you attempt to do this.
// See https://github.com/facebook/create-react-app/issues/240
new CaseSensitivePathsPlugin(),
// If you require a missing module and then `npm install` it, you still have
// to restart the development server for Webpack to discover it. This plugin
// makes the discovery automatic so you don't have to restart.
// See https://github.com/facebook/create-react-app/issues/186
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// Generate a manifest file which contains a mapping of all asset filenames
// to their corresponding output file so that tools can pick it up without
// having to parse `index.html`.
new ManifestPlugin({
fileName: "asset-manifest.json",
publicPath: publicPath
})
],
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
node: {
dgram: "empty",
fs: "empty",
net: "empty",
tls: "empty",
child_process: "empty"
},
// Turn off performance processing because we utilize
// our own hints via the FileSizeReporter
performance: false
};
start.js file:
"use strict";
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = "development";
process.env.NODE_ENV = "development";
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on("unhandledRejection", err => {
throw err;
});
// Ensure environment variables are read.
require("../config/env");
const fs = require("fs");
const chalk = require("chalk");
const webpack = require("webpack");
const WebpackDevServer = require("webpack-dev-server");
const clearConsole = require("react-dev-utils/clearConsole");
const checkRequiredFiles = require("react-dev-utils/checkRequiredFiles");
const {
choosePort,
createCompiler,
prepareProxy,
prepareUrls
} = require("react-dev-utils/WebpackDevServerUtils");
const openBrowser = require("react-dev-utils/openBrowser");
const paths = require("../config/paths");
const config = require("../config/webpack.config.dev");
const createDevServerConfig =
require("../config/webpackDevServer.config");
const useYarn = fs.existsSync(paths.yarnLockFile);
const isInteractive = process.stdout.isTTY;
// Warn and crash if required files are missing
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
process.exit(1);
}
// Tools like Cloud9 rely on this.
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
const HOST = process.env.HOST || "0.0.0.0";
if (process.env.HOST) {
console.log(
chalk.cyan(
` Attempting to bind to HOST environment variable: ${chalk.yellow(
chalk.bold(process.env.HOST)
)}`
)
);
console.log(
`If this was unintentional, check that you haven't mistakenly set it in your shell.`
);
console.log(
`Learn more here:`
);
console.log();
}
// We require that you explictly set browsers and do not fall
back to
// browserslist defaults.
const { checkBrowsers } = require("react-dev-utils/browsersHelper");
checkBrowsers(paths.appPath, isInteractive)
.then(() => {
// We attempt to use the default port but if it is busy, we offer the user to
// run on a different port. `choosePort()` Promise resolves to the next free port.
return choosePort(HOST, DEFAULT_PORT);
})
.then(port => {
if (port == null) {
// We have not found a port.
return;
}
const protocol = process.env.HTTPS === "true" ? "https" : "http";
const appName = require(paths.appPackageJson).name;
const urls = prepareUrls(protocol, HOST, port);
// Create a webpack compiler that is configured with custom messages.
const compiler = createCompiler(webpack, config, appName, urls, useYarn);
// Load proxy config
const proxySetting = require(paths.appPackageJson).proxy;
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
// Serve webpack assets generated by the compiler over a web server.
const serverConfig = createDevServerConfig(
proxyConfig,
urls.lanUrlForConfig
);
const devServer = new WebpackDevServer(compiler, serverConfig);
// Launch WebpackDevServer.
devServer.listen(port, HOST, err => {
if (err) {
return console.log(err);
}
if (isInteractive) {
clearConsole();
}
console.log(chalk.cyan("Starting the development server...\n"));
openBrowser(urls.localUrlForBrowser);
});
["SIGINT", "SIGTERM"].forEach(function(sig) {
process.on(sig, function() {
devServer.close();
process.exit();
});
});
})
.catch(err => {
if (err && err.message) {
console.log(err.message);
}
process.exit(1);
});
Error Stacktrace:
> project#0.1.0 start /usr/react_projects/project
> node scripts/start.js
Cannot read property 'tap' of undefined
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project#0.1.0 start: `node scripts/start.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
I don't understand why it is giving Cannot find property "tap" undefined.
I had the same exact problem. You have to add #babel/preset-react library to your package.json file.
Run yarn yarn add #babel/preset-react or npm install #babel/preset-react
I was trying to make a connection to the websocket and get the data in react router. The connection is not happening and getting error Uncaught SyntaxError: Failed to construct 'WebSocket': The URL '[object Object]' is invalid. in the browser console. Using npm start from the terminal to start the application. It is a simple react router application. Below is the react code where I think problem occurred.
import React from 'react'
export default React.createClass({
getInitialState: function() {
return { value : [],
client : '' };
},
componentWillMount: function() {
client = new WebSocket("ws://localhost:8000/","echo-protocol");
client.onerror = function() {
console.log('Connection Error');
};
client.onopen = function() {
function sendNumber() {
if (client.readyState === client.OPEN) {
var number = Math.round(Math.random() * 0xFFFFFF);
client.send(number.toString());
setTimeout(sendNumber, 1000);
}
}
sendNumber();
};
client.onmessage = function(e) {
this.setState({
value: e.data
});
}.bind(this);
},
componentWillUnmount: function(){
client.close();
},
render: function() {
return (React.createElement("div",null,this.state.value))
}
});
The webpack config file is -
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
publicPath: ''
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' }
]
}
}
Also, the packge.json file is
{
"name": "react_form",
"version": "1.0.0",
"description": "Sample form that uses React for its rendering",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --inline --content-base . --history-api-fallback"
},
"author": "J",
"license": "ISC",
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-router": "^2.0.0",
"websocket": "^1.0.23"
},
"devDependencies": {
"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"http-server": "^0.8.5",
"webpack": "^1.12.13",
"webpack-dev-server": "^1.14.1"
}
}
If any other piece of code is required then please let me know. Also, please find the image of the error coming in the console on selecting the route option. What is the exact issue I am not able to get ?
Try this syntax:
const client = new WebSocket("ws://localhost:8000/");
I was debugging the app in various different ways I could. Then I found that there is no problem with the code. Further changed the name of the file from WebSocket to Socket and route name from webSocket to socket in various files and it worked. The problem was with naming the files and including them into the code accordingly.
On your backend side, if you dont have header with key = "Sec-WebSocket-Protocol" and value of one of your protocols, you will always get this error on chrome.
In my case, it was fine with Firefox
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.