Problems using Netlify to host Gatsby built site - reactjs

Im getting this error
./node_modules/gatsby/cache-dir/gatsby-browser-entry.js
Module parse failed: Unexpected token (26:4)
You may need an appropriate loader to handle this file type.
|
| return (
| <React.Fragment>
| {finalData && render(finalData)}
| {!finalData && <div>Loading (StaticQuery)</div>}
gatsby-browswer-entry.js only has this inside of it:
import './src/styles/tailwind.css'
None of my .js files are failing to import the 'Link' component

Sometimes, depending on Gatsby's version and its dependencies, you need to import the component from gatsby-link rather than gatsby, so:
// import { Link } from "gatsby" // error
import Link from "gatsby-link" // not error

I had a similar issue, and was only exposed when running tests in cypress. I had used gatsby's navigate function in a non-jsx javascript helper file. I think the error is indicative of a bundling / webpack issue and you have to look at the stack trace to see the actual culprit file.

Related

Jest "cannot use import statement outside a module" error when testing Go JS PackedLayout in react app

I'm building a react app and was planning to using Go JS as a part of the app. However, I get this error when running the default yarn test that comes with create react app:
/path/frontend/node_modules/gojs/extensionsJSM/PackedLayout.js:11
import * as go from '../release/go-module.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | import * as go from 'gojs'
> 2 | import { PackedLayout } from 'gojs/extensionsJSM/PackedLayout'
| ^
3 | import { ReactDiagram } from 'gojs-react'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/components/Grid.tsx:2:1)
at Object.<anonymous> (src/App.tsx:6:1)
at Object.<anonymous> (src/App.test.tsx:4:1)
at TestScheduler.scheduleTests (node_modules/#jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/#jest/core/build/runJest.js:404:19)
I believe the bug appears because of the line import * as go from '../release/go-module.js'; in the Go JS PackedLayout file. I'm not quite sure as to how I am supposed to fix this. I assume that I'd have to somehow tell Jest to allow the import in the that particular file which is in node_modules.
I've tried adding transformIgnorePatterns: ['/node_modules/(?!gojs)'] to my jest.config.ts file but it did not fix the issue.
As documented at:
https://gojs.net/latest/intro/extensions.html
and at:
https://gojs.net/latest/api/symbols/PackedLayout.html,
you should copy the extension file into your project and update its import statement to refer to the same GoJS library file.

'ReferenceError: cptable is not defined' when running Jest tests, the actual code to generate the excel sheet works fine

The stack trace is as follows.
Test suite failed to run
ReferenceError: cptable is not defined
1 | import React, { Component } from 'react';
> 2 | import ReactExport from 'react-export-excel';
| ^
at cptable (node_modules/react-export-excel/node_modules/xlsx/xlsx.js:10:37)
at Object.<anonymous> (node_modules/react-export-excel/node_modules/xlsx/xlsx.js:6:1)
at Object.require (node_modules/react-export-excel/dist/ExcelPlugin/components/ExcelFile.js:19:13)
at Object.require (node_modules/react-export-excel/dist/index.js:7:18)
at Object.<anonymous> (components/CustomReport.jsx:2:1)
If i follow the trace, it reaches the following code section
at cptable (node_modules/react-export-excel/node_modules/xlsx/xlsx.js:10:37)
if(typeof cptable === 'undefined') cptable = require('./dist/cpexcel');
I've seen github issues where they suggest to remove "use strict" from component code but I'm not even using it. Here is the related issue https://github.com/SheetJS/sheetjs/issues/324
Well, this library didn't help so went with another one.
react-html-table-to-excel
Here, you need to create a html table and then that table gets converted to excel. I hid the table using CSS so that i only get the export excel button.

NextJS & CSS SyntaxError: Unexpected token

I'm trying to unit test but the only way I can stop the error throwing is to comment out the import './styles.css line.
As soon as I put it back in I get:
Jest encountered an unexpected token
...
SyntaxError: Unexpected token.
1 | import React from 'react';
2 | import PropTypes from 'prop-types';
> 3 | import './styles.css';
| ^
4 |
5 |
I have webpack, babel, jest, enzyme all configured but googling tells me there's a difference between running the app (via webpack) and using .css files vs running tests that can read .css files, which would need to be configured separately.
For love nor money, I cannot find an example where import './styles.css is successfully imported & the tests pass.
Can anyone help?
Managed to get this working by hitting up https://github.com/eddyerburgh/jest-transform-stub
My jest.config.js now looks like this:
module.exports = {
setupFiles: ['<rootDir>/jest.setup.js'], // links to normal "configure({ adapter: new Adapter() })" stuff.
testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'], // ignores test files in .next(js) & node modules
transform: {
"^.+\\.js$": "babel-jest", // anything .js is babel'd for jest to consume
"^.+.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "jest-transform-stub" // anything style related is ignored and mapped to jest-transform-stub module
},
moduleNameMapper: {
'\\.css$': '<rootDir>/EmptyModule.js' // <-- had to pop this in the following day to get any separetly imported .css files mapped to an empty module / object. So if i try to do import 'react-style-module/styles/my-styles.css' it would fail, though 'import './styles.css' worked. Now with this mapped correctly also, everything is imported/mapped/passing successfully.
}
}
If anyone else has other neater solutions, please let me know. x
In your package.json file set 'type' property to 'module'
{
"type":"module"
}
I think you declare like this:
import css from './styles.css'
<div className={css.test}>
</div>
Reference: https://github.com/zeit/next-plugins/tree/master/packages/next-css

Create-react-app doesn't allow import component.js from node_modules?

I received error when try to import { PrimaryButton } from 'my-npm-modules/src/button' ( which is a flowtype src jsx file with extension .js)?
Is it because the create-react-app have config to NOT do flowtype processing for files in node_modules?
Module parse failed: /Users/me/live-demo/node_modules/my-npm-modules/src/button.js Unexpected token (2:35)
You may need an appropriate loader to handle this file type.
| //#flow
| import React from 'react';
| export function PrimaryButton(props: {
| text: string;
| onClick: ()=>void
When I put the button.js inside the live-demo project, it works fine.
It seems it did exclude the node_modules folder for performance reason.
So I endup compile the flow-typed jsx --> normal js files, and provided js.flow at the same output folder for sake of consumer flowtype support.

Importing self-created libraries in reactjs

I'm using React and ES6 using babel and webpack. I am very new to this ecosystem.
I am trying to import some common utility functions into my jsx file but react is unable to find the file
homepage.jsx
var pathToRoot = './../..';
import path from 'path';
import React from 'react';
import ReactDOM from 'react-dom';
var nextWrappedIndex = require(path.join(pathToRoot,'/lib/utils.js')).nextWrappedIndex;
//some react/JSX code
utils.js
var nextWrappedIndex = function(dataArray) {
//some plain js code
return newIndex;
}
exports.nextWrappedIndex = nextWrappedIndex;
Directory structure is as follows:
src
|--app.js
|--components
| |--homepage
| |--homepage.jsx
|
|--lib
| |--utils.js
I am on a windows 10 machine and was facing issues during compilation providing the path by any other means. Using path.join solved compilation issue but the browser while rendering throws this error
Uncaught Error: Cannot find module '../../lib/utils.js'.
How do I accomplish this?
Also, is this the best way to do it(if altogether it is way it is supposed to be done in such ecosystem)?
One of the best and easiest way I have found in such a setup is to use Webpack aliases.
Webpack aliases will simply associate an absolute path to a name that you can use to import the aliased module from anywhere. No need to count "../" anymore.
How to create an alias?
Let's imagine that your Webpack config is in the parent folder of your src folder.
You would add the following resolve section in your config.
const SRC_FOLDER = path.join(__dirname, 'src')
resolve: {
alias: {
'my-utils': path.join(SRC_FOLDER, 'lib', 'utils')
}
}
Now, anywhere in your app, in any of your modules or React component you can do the following:
import utils from 'my-utils'
class MyComponent extends React.component {
render () {
utils.doSomething()
}
}
Small note about this method. If you run unit tests with a tool like enzyme and you don't run the component tested through Webpack, you will need to use the babel-plugin-webpack-alias.
More info on Webpack website: Webpack aliases
I solved this by replacing
var nextWrappedIndex = require(path.join(pathToRoot,'/lib/utils.js')).nextWrappedIndex;
with
import nextWrappedIndex from './../../lib/utils.js';
I tried to reproduce your code and Webpack printed me the following error:
WARNING in ./app/components/homepage/homepage.jsx
Critical dependencies:
50:0-45 the request of a dependency is an expression
# ./app/components/homepage/homepage.jsx 50:0-45
It means that Webpack couldn't recognize your require() expression because it works only with static paths. So, it discourages the way you are doing.
If you would like to avoid long relative paths in your import, I'd recommend you to set up Webpack.
First, you can set up aliases per Amida's answer.
Also, you can set up an extra module root via resolve.modules to make webpack look into your src folder, when you are importing something absolute, like lib/utils.js

Resources