How to use snap SVG in react with next and TS? - reactjs

Issue
Hello,
I am trying to use SnapSVG in my react project, but I cannot get it to run in the first place. Could someone help me with the proper settings? I am not very familiar with webpack so detailed instructions would be very much appreciated.
error
index.js?46cb:602 Uncaught ReferenceError: window is not defined
at Object.<anonymous> (FILEPATH/node_modules/snapsvg/dist/snap.svg.js:485:3)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.snapsvg (FILEPATH/.next/server/pages/index.js:1291:18)
at
**webpack_require**
(FILEPATH/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///./src/components/domain/top-first-view/index.tsx:9:65)
import in React components
import * as Snap from 'snapsvg';
package.json
"dependencies": {
"next": "^13.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sanitize.css": "^13.0.0",
"sass": "^1.56.1"
},
"devDependencies": {
"typescript": "^4.8.4",
"imports-loader": "^4.0.1",
"snapsvg": "^0.5.1",
"#types/snapsvg": "^0.5.2",
"cjs-loader": "^0.1.0",
...
},
"volta": {
"node": "18.6.0",
"yarn": "1.22.19"
}
what I've tried so far
I have tried using imports-loader as is discussed in this Github thread, but this resulted in a new error.
new error after webpack settings
Uncaught Error: ENOENT: no such file or directory, open 'FILEPATH/.next/server/pages/index.js'
at Object.openSync (node:fs:594:3)
at Object.readFileSync (node:fs:462:35)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1143:18)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.requirePage (FILEPATH/node_modules/next/dist/server/require.js:88:12)
at <unknown> (FILEPATH/Documents/GitHub/toda-web-art/node_modules/next/dist/server/load-components.js:37:73)
at async Object.loadComponents (FILEPATH/node_modules/next/dist/server/load-components.js:37:26)
webpack config in next.config.js
webpack(config) {
config.module.rules.push(
{
test: require.resolve('snapsvg'),
use: 'imports-loader?this=>window,fix=>module.exports=0',
},
...
Thank you!

Related

Issue during storybook setup "Cannot find module 'css-loader/lib/getLocalIdent'" in NextJs

I am facing an issue while building my project after storybook setup. I am getting an error in css-loaders that getLocalIdent is missing. This is the exact error
Error
{ Error: Cannot find module 'css-loader/lib/getLocalIdent'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (next.config.js:9:30)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3) code: 'MODULE_NOT_FOUND' }
This is the next config file
Next.config.js
const withSass = require("#zeit/next-sass");
const withCss = require("#zeit/next-css");
const gNextJsAppProperties = require("./next.properties");
const env = process.env.ENV || "local";
const defaultGetLocalIdent = require("css-loader/lib/getLocalIdent");
const localIdentName =
env === "local" || env === "dev"
? "[name]__[local]--[hash:base64:5]"
: "[hash:base64]";
const cssLoaderOptions = {
getLocalIdent: (loaderContext, localIdentName, localName, options) => {
if (loaderContext.resourcePath.includes("node_modules")) {
return localName;
} else {
return defaultGetLocalIdent(
loaderContext,
localIdentName,
localName,
options
);
}
},
localIdentName
};
module.exports = withSass(
withCss({
cssLoaderOptions,
distDir: "dist",
cssModules: true
})
);
This is my package.json file with the dependencies.
Package.json
{
"name": "test",
"dependencies": {
"#zeit/next-sass": "^1.0.1",
"next": "^9.4.4",
"node-sass": "^4.14.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"sass": "^1.29.0"
},
"devDependencies": {
"#storybook/preset-typescript": "^3.0.0",
"#storybook/react": "^6.1.2",
"#types/jest": "^25.2.3",
"#types/node": "^12.12.50",
"#types/react": "^16.9.43",
"#types/react-dom": "^16.9.8",
"babel-loader": "^8.2.1",
"babel-preset-react-app": "^10.0.0",
"body-scroll-lock": "^2.7.1",
"copy-webpack-plugin": "^6.1.0",
"css-loader": "^4.2.0",
"sass-loader": "^10.1.0",
"style-loader": "^2.0.0",
},
"scripts": {
"storybook": "start-storybook -p 6006 -c .storybook"
}
}
As much as I have checked online most of the places its written that this getLocalIdent has been moved in css-loader but all the solutions suggested are not working for me.
It would be great if someone can suggest a fix for this as I am clueless on how to fix this.

Yarn encore prod failing

Im trying to deploy a symfony application on my Ubuntu server.
Mind the following details.
It is a simple application but some yarn packages seem to be failing.
What can I do?
Im working with Symfony 5.
My package.json
{
"devDependencies": {
"#symfony/webpack-encore": "^0.30.2",
"bootstrap": "^4.5.2",
"jquery": "^3.5.1",
"node-sass": "^4.14.1",
"popper.js": "^1.16.1",
"webpack-notifier": "^1.6.0"
},
"dependencies": {
"add": "^2.0.6",
"copy-webpack-plugin": "^6.0.3",
"sass-loader": "^8.0.0",
"yarn": "^1.22.4"
},
"name": "fluid-cms",
"version": "1.0.0",
"main": "index.js",
"repository": "---",
"author": "---",
"license": "MIT"
}
The following output comes out.
I have tried many things.
Ubuntu 18.04.
alpha#ovhBrains:/var/www/fluid-cms$ sudo yarn encore prod
yarn run v1.22.4
$ /var/www/fluid-cms/node_modules/.bin/encore prod
Running webpack ...
/var/www/fluid-cms/node_modules/copy-webpack-plugin/node_modules/p-limit/index.js:30
} catch {}
^
SyntaxError: Unexpected token {
at NativeCompileCache._moduleCompile (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-cache.js:242:18)
at Module._compile (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-cache.js:186:36)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
at Object.<anonymous> (/var/www/fluid-cms/node_modules/copy-webpack-plugin/dist/index.js:10:38)
at Module._compile (/var/www/fluid-cms/node_modules/v8-compile-cache/v8-compile-
d.
---
Another error:
#symfony/webpack-encore#0.30.2: The engine "node" is incompatible with this module. Expected version ">= 10.13.0". Got "8.10.0"
Once I found out the second error log it was easy to fix.
My nodejs -v was not matching with the requirements.

Installing React-native calendars breaks installation

I'm a bit puzzled why two different npm installs for a react-native calendar are breaking the solution. I am running react-native through expo, and it runs fine before installing the calendar.
But after installing either
react-native-general-calendars
or
react-native-calendars
I get this error in the console after running
expo start:
> [17:30:29] Starting project at --location--
[17:30:33] Error: React native is not installed. Please run `npm install` in your project directory.
[17:30:33] Couldn't start project. Please fix the errors and restart the project.
[17:30:33] Set EXPO_DEBUG=true in your env to view the stack trace.
I run npm install and get the following error:
> [17:33:34] Starting project at --location--
[17:33:40] --location--\node_modules\react-native\local-cli\server\checkNodeVersion.js:43
[17:33:40] );
[17:33:40] ^
[17:33:40] SyntaxError: Unexpected token )
[17:33:40] at createScript (vm.js:56:10)
[17:33:40] at Object.runInThisContext (vm.js:97:10)
[17:33:40] at Module._compile (module.js:542:28)
[17:33:40] at Object.Module._extensions..js (module.js:579:10)
[17:33:40] at Module.load (module.js:487:32)
[17:33:40] at tryModuleLoad (module.js:446:12)
[17:33:40] at Function.Module._load (module.js:438:3)
[17:33:40] at Module.require (module.js:497:17)
[17:33:40] at require (internal/module.js:20:19)
[17:33:40] at Object.<anonymous> (--location--\node_modules\react-native\local-cli\cli.js:17:1)
[17:33:40] Metro Bundler process exited with code 1
[17:33:40] Set EXPO_DEBUG=true in your env to view the stack trace.
Here is my package.json:
{
"name": "my-new-project",
"main": "node_modules/expo/AppEntry.js",
"private": true,
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"test": "node ./node_modules/jest/bin/jest.js --watchAll"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"#expo/samples": "2.1.1",
"expo": "^29.0.0",
"react": "^16.4.2",
"react-native": "^0.56.0",
"react-native-general-calendars": "^1.7.2",
"react-navigation": "^2.9.3"
},
"devDependencies": {
"jest-expo": "^29.0.0"
}
}
When I uninstall the calendar I still get the error now. Not sure how to get expo to stop throwing the error. Anyone know how to fix this?

Testing with React

I can't seem to test my files anymore.
I "npm i react react-dom react-addon-test-utils"
but even after all those installs,
it tells me i'm missing something
i even tried to install both devdep. and dep.
module.js:339
throw err;
^
Error: Cannot find module 'react-dom/lib/ReactTestUtils'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/react-addons-test-utils/index.js:1:80)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at /Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/react-compat.js:92:17
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/react-compat.js:146:5)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/Utils.js:29:20)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/MountedTraversal.js:32:14)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/ReactWrapper.js:23:25)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/enzyme/build/index.js:15:21)
at Module._compile (module.js:425:26)
at Module._extensions..js (module.js:432:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (AddNewSong.spec.js:5:28)
at Module._compile (module.js:425:26)
at loader (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:128:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/babel-register/lib/node.js:138:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at /Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/lib/mocha.js:216:14)
at Mocha.run (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/lib/mocha.js:468:10)
at Object.<anonymous> (/Users/Kainoa/DevLeague/kanakaHacks_Software/KanakaMusicR/kanakamusicR_A/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
npm ERR! Test failed. See above for more details.
this is the package.json
{
"name": "KMR",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"start": "node app.js",
"build": "webpack",
"watch": "webpack --watch",
"test": "mocha --require test/helpers/setup.js",
"lint": "eslint --ignore-path .gitignore --cache ./",
"cover": "nyc --reporter=lcov --reporter=text --reporter=html --require babel-register --extension .jsx npm test"
},
"license": "MIT",
"dependencies": {
"axios": "0.9.1",
"bluebird": "^3.4.6",
"errorhandler": "^1.4.3",
"express": "4.13.4",
"express-favicon": "^2.0.0",
"express-jwt": "^5.1.0",
"lodash": "4.6.1",
"method-override": "^2.3.6",
"mysql": "^2.11.1",
"node-ensure": "0.0.0",
"pg": "^6.1.0",
"pg-hstore": "^2.3.2",
"pg-promise": "^5.3.4",
"react": "0.14.7",
"react-dom": "0.14.7",
"react-modal": "^1.5.2",
"react-redux": "4.4.0",
"react-responsive": "^1.2.5",
"react-router": "2.0.0",
"redux": "3.3.1",
"sequelize": "^2.1.3",
"sequelize-cli": "^2.4.0",
"sqlite3": "^3.1.6",
"webpack": "^1.13.2",
"webpack-dev-middleware": "^1.8.4",
"webpack-hot-middleware": "^2.13.0"
},
"devDependencies": {
"babel-core": "6.5.2",
"babel-loader": "6.2.2",
"babel-polyfill": "6.7.4",
"babel-preset-es2015": "6.5.0",
"babel-preset-react": "6.5.0",
"babel-register": "6.5.2",
"chai": "3.5.0",
"enzyme": "2.0.0",
"eslint": "2.2.0",
"eslint-config-standard": "5.1.0",
"eslint-config-standard-jsx": "1.1.1",
"eslint-config-standard-react": "2.3.0",
"eslint-loader": "1.3.0",
"eslint-plugin-promise": "1.0.8",
"eslint-plugin-react": "4.1.0",
"eslint-plugin-standard": "1.3.2",
"gulp": "^3.9.1",
"gulp-connect": "^5.0.0",
"gulp-sass": "^2.3.2",
"jsdom": "8.0.4",
"json-loader": "0.5.4",
"livereload": "^0.6.0",
"mocha": "2.4.5",
"nyc": "6.0.0",
"react": "^0.14.7",
"react-addons-test-utils": "^15.4.1",
"react-dom": "^0.14.7",
"sinon": "^1.17.3",
"supertest": "^2.0.1",
"webpack": "1.13.2"
},
"directories": {
"test": "test"
},
"keywords": [
"react",
"workshop",
"intro",
"redux"
]
}
I found a work around for this from here
make sure that your app contains follow dependencies all in the same
version in package.json
react-addons-test-utils
react
react-dom
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-dom": "^0.14.7",
or upgrade to latest versions of above dependencies
For anyone who has run into this problem lately, note that react-addons-test-utils have been deprecated and all the test methods have been moved into react-dom.
So the fix looks like:
- Remove react-addons-test-utils from package.json and run npm update.
- Change the import statements to reflect the updates. For example:
import { renderIntoDocument } from 'react-dom/test-utils';
instead of the old
import { renderIntoDocument } from 'react-addons-test-utils';

Unable to import a React Component in my Mocha test

I"m trying to do a simple Mocha test, which works, but once i start trying to add my own components for testing, it is unable to resolve the module.
package.json:
{
"name": "My Project",
"version": "0.1.0",
"scripts": {
"test": "mocha \"./src/**/*.spec.js\""
},
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7",
"webpack": "^1.12.12"
},
"devDependencies": {
"bower": "^1.4.1",
"expect": "^1.14.0",
"expect-jsx": "^2.3.0",
"jsx-loader": "^0.13.2",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7"
}
}
src/dashboard/TestComponent.jsx:
var React = require('react');
var TestComponent = React.createClass({
render: function() {
return (
<div />
);
}
});
module.exports = TestComponent;
src/dashboard/test.spec.js:
var React = require("react");
var ReactTestUtils = require('react-addons-test-utils');
var expect = require("expect");
var expectJSX = require("expect-jsx");
expect.extend(expectJSX);
var TestComponent = require('./TestComponent');
describe('Test', function() {
it('should work', function() {
expect(true).toEqual(true)
});
});
If i remove the line var TestComponent = require('./TestComponent.jsx');, my spec passes. But once i try to require my component, it gives me this error:
$ npm test
>My Project#0.1.0 test C:\workspace\root
>mocha "./src/**/*.spec.js"
module.js:338
throw err;
^ Error: Cannot find module './TestComponent'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (C:\workspace\root\src\dashboard\test.spec.js:7:22)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at C:\workspace\root\node_modules\mocha\lib\mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (C:\workspace\root\node_modules\mocha\lib\mocha.js:216:14)
at Mocha.run (C:\workspace\root\node_modules\mocha\lib\mocha.js:468:10)
at Object.<anonymous> (C:\workspace\root\node_modules\mocha\bin\_mocha:403:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3 npm ERR! Test failed. See above for more details.
Mocha probably can't read your TestComponent because it contains syntax unfamiliar to it (the JSX). You'll need to specify a compiler for mocha as well as babel presets (assuming you're using Babel).
Assuming you already have babel-core and babel-preset-react installed for your project, make these changes in your package.json:
"scripts": {
"test": "mocha --compilers js:babel-register \"./src/**/*.spec.js\""
},
"babel": {
"presets": ["react"]
}
You should add the .jsx extension to your require call, as by default it won't recognize .jsx files as modules.
Rick is also right about the compiler but in this case the error is due to the synthax of require. Then if you fix the require you will see an error of unexpected token, which is the lack of compiler.
You should therefore correct both points.

Resources