ERROR in ./~/react-network-diagrams/lib/map.css - reactjs

I am wanting to use the react-network-diagrams library. I have node v0.10.25, npm version 1.3.10 installed and am able to run .js files using node. I have followed tutorials to install webpack and babel and these both appear to be working with the tutorial examples. I have installed the react-network-diagram components using npm. This initially complained that my sibling dependencies we wrong. This seamed to be fixed when I reloaded the react and react-dom and gave a specific version (npm install --save react#^0.14.3 react-dom#^0.14.3)
So everything seems to work according to the tutorials.
Here is the index.jsx file contents:
console.log( "hw" )
import React from 'react';
import {render} from 'react-dom';
import { TrafficMap } from "react-network-diagrams";
class App extends React.Component {
render () {
return <TrafficMap width={980} height={500} margin={50}
topology={topo}
traffic={traffic}
edgeColorMap={edgeColorMap}
edgeDrawingMethod="bidirectionalArrow"
edgeThinknessMap={edgeThinknessMap}
edgeShapeMap={edgeShapeMap}
nodeSizeMap={nodeSizeMap}
nodeShapeMap={nodeShapeMap}
stylesMap={stylesMap}
selection={mapSelection}
onSelectionChange={this.handleSelectionChanged} />
//return <p> Hello React!</p>;
}
}
render(<App/>, document.getElementById('app'));
And here is output of webpack
ubuntu#ip-172-31-38-114:~/doe2$ ./node_modules/.bin/webpack -d
Hash: 1ecd317731574aa4e7fd
Version: webpack 1.13.0
Time: 5901ms
Asset Size Chunks Chunk Names
bundle.js 1.38 MB 0 [emitted] main
bundle.js.map 1.55 MB 0 [emitted] main
+ 204 hidden modules
ERROR in ./~/react-network-diagrams/lib/map.css
Module parse failed: /home/ubuntu/doe2/node_modules/react-network-diagrams/lib/map.css Unexpected token (1:4)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (1:4)
at Parser.pp.raise (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:920:13)
at Parser.pp.unexpected (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:1483:8)
at Parser.pp.semicolon (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:1462:73)
at Parser.pp.parseExpressionStatement (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:1976:8)
at Parser.pp.parseStatement (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:1754:188)
at Parser.pp.parseTopLevel (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:1648:21)
at Parser.parse (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:1616:17)
at Object.parse (/home/ubuntu/doe2/node_modules/webpack/node_modules/acorn/dist/acorn.js:882:44)
at Parser.parse (/home/ubuntu/doe2/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/home/ubuntu/doe2/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/home/ubuntu/doe2/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/home/ubuntu/doe2/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /home/ubuntu/doe2/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.finished (/home/ubuntu/doe2/node_modules/webpack/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at /home/ubuntu/doe2/node_modules/webpack/node_modules/enhanced-resolve/node_modules/graceful-fs/graceful-fs.js:78:16
at fs.js:268:14
at /home/ubuntu/doe2/node_modules/webpack/node_modules/enhanced-resolve/node_modules/graceful-fs/graceful-fs.js:43:10
at Object.oncomplete (fs.js:107:15)
# ./~/react-network-diagrams/lib/map-base.js 55:0-20
The information given in the react network diagrams website (http://software.es.net/react-network-diagrams/#/?_k=xs005u) is unclear to me. Under the heading Examples it says I must run npm install. I have done this is several different directories but it hasn't helped and I am working somewhat in the dark. npm run web-site is a script within the package and it seems to do something but then it says to connect with a webclient to port 8080 but there is nothing listening there.
I just feel like I missing one or two crucial bits to bring the whole thing together.
Thanks for help.
Ian.

It looks like you are missing webpack's css loader configuration.
1) First install the css-loader (imports the css), and style-loader (adds the css to the DOM)
npm install css-loader style-loader --save-dev
2) Add the loader configuration to your webpack config
{
// ...
module: {
loaders: [
{ test: /\.css$/, loader: "style!css" }
]
}
}
You could find some more information here Webpack: EMBEDDED STYLESHEETS

Promises are part of ES2015 (previously ES6). They are not yet integrated into all environments, including yours. There is a very well-known library that adds support for new ecmascript features, which is called Babel.js.
Just like we added a css-loader, we are going to add a babel-loader:
npm install babel-loader babel-core babel-preset-es2015 --save-dev
Now add to your loaders:
{
// ...
module: {
loaders: [
{ test: /\.css$/, loader: "style!css" },
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel', // 'babel-loader' is also a legal name to reference
query: {
presets: ['es2015']
}
}
]
}
}
For more information: babel-loader

Related

Upgrade to Webpack 5 breaking Storybook 5

In process of upgrading a webpack 4/storybook 5 project to webpack 5 to hopefully take advantage of federated modules. I have regular webpack --config webpack.config.js building working atfer some struggle, but I can't seem to overcome this storybook config issue to get that working. there's not a lot in the storybook webpack.config.js - just some module rules for testing for less files and using the appropriate loaders. It seems the error I'm encountering is typical when upgrading webpack majors, as I've found a number of folks that encountered the same thing going from 3-4, but anything I've tried has failed so far. The specific stacktrace is:
Cannot read property 'tapAsync' of undefined
at ExternalModuleFactoryPlugin.apply (/Users/tbullard/Workspace/unify/node_modules/webpack/lib/ExternalModuleFactoryPlugin.js:29:39)
at compiler.hooks.compile.tap (/Users/tbullard/Workspace/unify/node_modules/webpack/lib/ExternalsPlugin.js:24:63)
at SyncHook.eval [as call] (eval at create (/Users/tbullard/Workspace/unify/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:1)
at SyncHook.lazyCompileHook (/Users/tbullard/Workspace/unify/node_modules/tapable/lib/Hook.js:154:20)
at hooks.beforeCompile.callAsync.err (/Users/tbullard/Workspace/unify/node_modules/#storybook/core/node_modules/webpack/lib/Compiler.js:665:23)
at _err0 (eval at create (/Users/tbullard/Workspace/unify/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
at compiler.inputFileSystem.readFile (/Users/tbullard/Workspace/unify/node_modules/#storybook/core/node_modules/webpack/lib/DllReferencePlugin.js:72:15)
at process.nextTick (/Users/tbullard/Workspace/unify/node_modules/#storybook/core/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:85:15)
at process._tickCallback (internal/process/next_tick.js:61:11)
Leads me to believe there's a plugin incompatibility 🤷🏻‍♂️ TIA!
As of 6.2.0, Storybook's preview builder is officially Webpack 5 compatible and the manager builder is unofficially Webpack 5 compatible. See this explanation of the builders and Webpack 5 compatibility. Also check out this gist/comments section with more detailed installation instructions.
If I understand correctly, setting the builder to Webpack 5 (as per these instructions) will force the preview builder to use Webpack 5, allowing you to expose your UI components for cool, new Webpack 5 features such as module federation.
However, if you also want to force the manager builder to use Webpack 5 (so that you can just finish breaking up with Webpack 4), you'll need to make use of Yarn selective dependency resolutions. Here is what I did specifically (in a package.json):
"resolutions": {
"webpack": "^5.27.2",
"css-loader": "^5.0.1",
"dotenv-webpack": "^7.0.2",
"html-webpack-plugin": "^5.0.0",
"style-loader": "^2.0.0",
"terser-webpack-plugin": "^5.1.1",
"webpack-dev-middleware": "^4.1.0",
"webpack-virtual-modules": "^0.4.2"
}
With these resolutions, Yarn detects that Webpack 4 is no longer used and removes it. The SB build gives this warning:
info #storybook/react v6.2.0-rc.10
info
info => Loading presets
WARN Unexpected webpack version in manager-builder
WARN - Received: 5.27.2
WARN - Expected: 4.x
One of the tasks of migrating from Webpack 4 to 5 involves manually providing browser polyfills for node packages which were automatically provided by Webpack 4. I want to note that if you find yourself manually providing a ton of polyfills while upgrading Storybook to Webpack 5, you have probably gotten off in the wrong direction. The Storybook dev-server builds get cached in a local (to the package where Storybook is installed) node_modules directory (whatever-package/node_modules/.cache/storybook/dev-server). Deleting the dev-server sub-directory regularly can help you debug your build and potentially spare you from building out long lists of unnecessary node polyfills. (I learned this the hard way).
With that said, for a cleanish install of Storybook you might not actually need any polyfills. On the other hand, some cases do require the node polyfills (e.g. #storybook/addon-docs requires "assert" (see below)). Here is one way to add polyfills (and addons, if you want) to Storybook's Webpack config in main.js:
module.exports = {
core: {
builder: 'webpack5',
},
stories: ['../whatever/src/**/*.stories.#(ts|tsx)'],
addons: [
'#storybook/addon-actions',
'#storybook/addon-controls',
'#storybook/addon-docs',
],
webpackFinal: (config) => {
return {
...config,
resolve: {
...config.resolve,
fallback: {
...config.fallback,
assert: require.resolve('assert-browserify/'),
},
},
};
},
};
Re: addons, I had serious issues with node polyfills when attempting to use addon-essentials. I've been... adding... addons piecemeal instead (standalone packages via npm), and that seems to be working without any polyfills (#storybook/actions and #storybook/controls are good oob; #storybook/docs requires the assert polyfill (above), #storybook/addons is also working fine with theming in manager.ts---that is:
import { addons } from '#storybook/addons';
import { themes } from '#storybook/theming';
addons.setConfig({
theme: themes.dark,
});
I also want to note that adding sass-loader to Webpack config.module.rules works as expected. Some people were running into problems with some scss preset with Storybook and Webpack 5. Here's the relevant portion of a proper Storybook Webpack config for Sass:
module: {
...config.module,
rules: [
...config.module.rules,
{
test: /\.(scss)$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: function () {
return [require('precss'), require('autoprefixer')];
},
},
},
},
{
loader: require.resolve('sass-loader'),
options: {
// using sass (Dart) instead of node-sass because node-sass (Javascript) cannot resolve Yarn 2's Plug'N'Play synthetic node_modules directory
// Evidently, sass is a bit slower to compile than node-sass, but I think I prefer sass anyway for latest features (such as #use)
implementation: require('sass'),
},
},
],
},
],
},
Hope that will get you off the ground 🛫
Storybook is not yet ready to work with Webpack 5 but it is on their roadmap for version 7.0.
More context in this GitHub issue.
Upgrading webpack v4 to v5 in storybook v6.
check the below link for detailed explanation.
https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#webpack-5
"#storybook/builder-webpack5": "^6.5.15",
"#storybook/manager-webpack5": "^6.5.15",
It happened to me as well, in the end I solved it setting the dependency for storybook webpack5 but using webpack4:
"#storybook/addon-actions": "^6.2.9",
"#storybook/addon-controls": "^6.2.9",
"#storybook/addon-storysource": "^6.2.9",
"#storybook/builder-webpack5": "^6.2.9",
"#storybook/vue": "^6.2.9",
As I read here: https://stackoverflow.com/a/67075112/5384339 I think it's better to wait a bit before using webpack5

Getting an error "Module not found: Can't resolve './User.scss' " when running heroku-postbuild

I am writing a chat using React/Redux/Firebase
I have next structure:
components
User
index.js
User.scss
UserHeader.js
UserItem.js
UserList.js
UserSerach.js
Message
index.js
Message.scss
MessageList.js
...
All styles of components of "type" User are located in the file User.scss
I'm not sure what's right. I don't like a lot of folders with components
My actions:
git add .
git commit --amend --no-edit
heroku create
heroku git:remote -a project-name
git push heroku master
In the file package.json i also write:
"engines": {
"npm": "6.4.1",
"node": "10.14.2"
}
And to start production after installing packages:
"heroku-postbuild": "npm run serve"
During heroku package installation I get an error:
ERROR in ./src/components/User/UserHeader.js
remote: Module not found: Error: Can't resolve './User.scss' in '/tmp/build_0467563cc44feeb52e4f8bb96c6f75fa/src/components/User'
remote: # ./src/components/User/UserHeader.js 3:0-33 8:15-21 10:15-21 14:15-21 16:15-21 19:15-21
remote: # ./src/components/User/index.js
remote: # ./src/components/Chat/Chat.js
remote: # ./src/components/Chat/index.js
remote: # ./src/components/App/App.js
remote: # ./src/components/App/index.js
remote: # ./src/index.js
But I imported the User.scss:
import styles from './User.scss';
The same error pops out for other User components. But I have same structure with one scss file in Message components and there are no errors..
Styles in the webpack config:
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
}
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: () => [
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9'
],
flexbox: 'no-2009'
})
]
}
},
'sass-loader'
]
}
I have already tried several other style settings - the same error.
Edit
I found out that the error was in the scss file name (user.scss => User.scss). But I'm getting the same error.
I had a mistake that I fixed after reading that the webpack or the Windows console or the Heroku (I don't remember) caches files. And when you change their location or the names for the Heroku they don't change. Something was like that but I don't remember.
I need to enter something in the console so that changes to files and paths are taken into account. Maybe someone knows?
Edit 2
Solved my stupid problem:
Made a commit on the GitHub by changing the name of the files to the correct ones. It's not working if I change files locally, because of the problem that I wrote above)
Made a git pull
Now the Heroku works :)
But I still want to know how to solve the problem written above.

Syntax Error In IE 11 for this node_moduels

I am getting a syntax error in IE when this component of react is loaded in the webpage. Has anybody got the same problem? This is an inherited package, and a syntax error from node_modules makes no sense?
"use strict";
/* WEBPACK VAR INJECTION */(function(module) {
const colorConvert = __webpack_require__(/*! color-convert */ "./node_modules/color-convert/index.js");
const wrapAnsi16 = (fn, offset) => function () {
const code = fn.apply(colorConvert, arguments);
return `\u001B[${code + offset}m`;
};
const wrapAnsi256 = (fn, offset) => function () {
const code = fn.apply(colorConvert, arguments);
return `\u001B[${38 + offset};5;${code}m`;
};
If you are using newer versions of Node/NPM, check your package.json file -> "browserslist" section.
This is the default "browserslist" created for you if you do not have one defined:
In this case, if you run "npm start" on your LOCAL Environment, Babel will not create Polyfills for IE11 because its not included as a target browser in "development". To get this working, I deleted my node_modules directory completely, ran 'npm install', updated package.json with:
and ran 'npm start.
The reason why this fails is that babel or your other favorite transpiler might ignore node_modules (if that's how its configured), so you need to include it manually because IE does not support arrow function syntax.
First, if you search for wrapAnsi16 or wrapAnsi256 function names online it'll point you to common npm packages, such as: ansi-styles, chalk or color-convert, debug, strip-ansi, etc.
If you are using Webpack you can add the following to your rules:
module: {
rules: [{
exclude: /node_modules\/(?!(color-convert|ansi-styles|strip-ansi|ansi-regex|debug|react-dev-utils|chalk)\/).*/
}]
}
or, easier to read:
module: {
rules: [{
include: [
path.resolve(__dirname, 'node_modules/ansi-styles'),
path.resolve(__dirname, 'node_modules/strip-ansi'),
... other's here...
path.resolve(__dirname, 'src'),
]
}]
}
Hope this helps somebody in the future ;)
TLDR; you don't need this library, just run
npm run build
And it will be excluded from your build.
I have same problem with create-react-app, and I solve it (no). From my discovery, this library should not appear in browser, because it was designed for nodejs environment. Also I found, this library come to me as dependency of jest, and jest is dependency for tests and it come as dependency for react.
So, I run
npm run build
server -s build
And try my application in IE. And it work. So, when you run
npm start
It make file including dev dependencies and other garbage that should not appear in production and in browser at all. When you run
npm run build
It make file only with required project libraries.
I had similar issue #punkbit solution and installing 'react-app-polyfill'
and importing it at the top of the index.js file solved it
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
If it still does not work delete node-modules and reinstall also clear cache in IE.
All the best :)
This problem occurs because your compiled code contains (modern) ES6 syntax whilst IE11 only supports ES5.
A way to fix this is to instruct webpack to specifically compile the mentioned packages into ES5;
module: {
rules: [{
test: /\.(tsx?|js)$/,
include: [
// These dependencies have es6 syntax which ie11 doesn't like.
// Whenever you see a "SyntaxError" that crashes IE11 because of a new lib, add it here.
path.join(__dirname, 'node_modules/react-intl'),
path.join(__dirname, 'node_modules/pkce-challenge'),
path.join(__dirname, 'node_modules/fuse.js')
],
use: [{
loader: 'ts-loader', // Or whatever loader you're using
}]
}]
}
for me this was: fuse.js, pkce-challenge and react-intl.

Webpack load module outside the root directory

I am using Webpack to build my react-redux apps and encountered a situation:
I tried to take some common components out of one app directory in order to reuse them during multiple react app, but when I tried to load one component outside the app directory I got errors:
Module build failed: SyntaxError: D:/dev/gdas/WebContent/reactx/common/components/HeaderFooter/Header.js: Unexpected token (74:31)
My babel-loader rule in webpack.config.js is as below:
{
test: /\.jsx?$/,
include: [path.resolve(__dirname, 'src'), path.resolve(__dirname, '../reactx/common/components')],
loader: 'babel-loader'
}
and My import code is as below:
import Header from '../../../reactx/common/components/HeaderFooter/Header.js'
Is that any solution to fix this issue?
You're doing everything right. It's just that this file D:/dev/gdas/WebContent/reactx/common/components/HeaderFooter/Header.js has a SyntaxError at 74:31

Angular 2 and Webpack - url() in SASS file cannot resolve even with resolve-url-loader

I started my Angular 2 Webpack project from this official guide.
I went ahead and ran npm install --save-dev node-sass sass-loader resolve-url-loader. That went well.
My component is sitting at root/src/app/mycomponent/mycomponent.ts, and I'm referencing my scss in the heading this way:
#Component({
selector: 'home',
templateUrl: './mycomponent.html',
styleUrls: ['./mycomponent.scss']
})
The scss file is at root/src/app/mycomponent/mycomponent.scss and I have the following SASS in there:
p{
color: blue;
background-image: url('./background.jpg');
}
That image is sitting at root/src/app/mycomponent/background.jpg.
In webpack.common.js I have the following rule for .scss files:
{
test: /\.scss$/,
exclude: 'node_modules',
loaders: ['style-loader','css-loader','resolve-url-loader','sass-loader?sourceMap']
}
as recommended by resolve-url-loader documentation.
I keep on getting this error:
ERROR in ./~/css-loader!./~/resolve-url-loader!./~/sass-loader/lib/loader.js?sourceMap!./src/app/mycomponent/mycomponent.scss
Module not found: Error: Can't resolve './background.jpg'
I even tried loaders: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader!sass-loader?sourceMap!resolve-url-loader'}) and I'm still getting the same error.
For the life of me, I can't figure out what I did wrong. I was under the impression that resolve-url-loader will let me put a location in SASS url() relative to the .scss file it's declared in! That background.jpg sits in the same folder as my .scss file, so I think I referenced the path correctly.
I searched SO and the web and nobody seems to have the right solution. Is there any other code I need to show? Please help! And please, I don't want to import or require image files from .ts.
Update
It turns out that I misspelled the name of the image's file name. Now, I don't get that "module not found" error.
HOWEVER I now get the error in the browser's console output: Expected 'styles' to be an array of strings. This is with the configuration
loaders: ['style-loader','css-loader','resolve-url-loader','sass-loader?sourceMap']
Still stuck.
According to the error message, Angular is expecting assets to be strings while resolving the code. Thus in this situation, we need another loader here:
npm install --save-dev css-to-string-loader
Then in your webpack config file:
{
test: /\.scss$/,
exclude: /node_modules/,
loaders: ['css-to-string-loader', 'css-loader', 'resolve-url-loader', 'sass-loader?sourceMap']
},
It should be good to go now.
I managed to get passed this issue by using 'exports-loader' instead of 'style-loader'
eg:
'exports-loader?module.exports.toString()'

Resources