Cannot find MDX module in Gatsby - reactjs

I'm making a Gatsby site, and I've intially set it up to use markdown files via gatsby-transformer-remark. I've since decided to move it over to MDX in order to have a bit more freedom in what content I can use.
I've followed this guide to migrate the project from MD to MDX, but I keep getting the following error:
Cannot find module 'D:\XXXX\XXXX\gatsby\.cache\caches\gatsby-plugin-mdx\webpack\output.js'
Require stack:
- D:\XXXX\XXXX\gatsby\node_modules\gatsby-plugin-mdx\utils\render-html.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby-plugin-mdx\gatsby\source-nodes.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby-plugin-mdx\gatsby-node.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\bootstrap\load-plugins\validate.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\bootstrap\load-plugins\load.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\bootstrap\load-plugins\index.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\services\initialize.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\services\index.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\state-machines\develop\services.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\state-machines\develop\index.js
- D:\XXXX\XXXX\gatsby\node_modules\gatsby\dist\commands\develop-process.js
- D:\XXXX\XXXX\gatsby\.cache\tmp-4452-lp3aICfNr8bo
I've tried:
Clearing cache and build files (gatsby clean)
Updating all packages, including Gatsby itself
Various ways to include the plugin in gatsby-config.js
Changing it all back to MD, making sure it works, and then migrating it to MDX once again.
Doing a build. Same thing happens as when running gatsby develop
Deleting package-lock.json and node_modules folder and running npm install again.
Nothing worked. This has to be something really stupid to do with NPM or Webpack but I just don't see it.
gatsby-config.js
module.exports = {
siteMetadata: {
title: "XXXX",
copyright: "XXXX"
},
plugins: [
{
resolve: `gatsby-plugin-mdx`,
options: {
extensions: [`.md`, `.mdx`],
},
},
`gatsby-transformer-remark`,
`gatsby-plugin-image`,
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `src`,
path: `${__dirname}/src/`,
},
},
]
}
package.json
{
"name": "gatsby-starter-hello-world",
"private": true,
"description": "A simplified bare-bones starter for Gatsby",
"version": "0.1.0",
"license": "0BSD",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"dependencies": {
"#mdx-js/mdx": "^1.6.22",
"#mdx-js/react": "^1.6.22",
"gatsby": "^3.1.2",
"gatsby-plugin-image": "^1.1.2",
"gatsby-plugin-mdx": "^2.1.0",
"gatsby-plugin-sharp": "^3.1.2",
"gatsby-source-filesystem": "^3.1.0",
"gatsby-transformer-remark": "^3.1.0",
"gatsby-transformer-sharp": "^3.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-in-viewport": "^1.0.0-alpha.16",
"react-reveal-text": "^0.1.1",
"react-spring": "^8.0.27",
"react-text-loop": "^2.3.0"
},
"devDependencies": {
"prettier": "2.2.1"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-hello-world"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
EDIT:
I should point out that when I go that location in cache that's mentioned in the error, there really is no webpack folder, nor output.js - so it really isn't being generated when I run gatsby develop, I just can't understand why.
I've also tried making a new project by cloning the offical gatsby mdx starter template, and that is working just fine out of the box.

Related

Monorepo with Lerna & Next.JS not working : Module not found

Not sure what I am doing wrong. I am trying to setup a monorepo with lerna & nextjs and have been following a tutorial but get stuck at this point.
My file structure looks exactly as specified in the tutorial:
lerna.json
package.json
packages
--- components (React Components)
--- package.json
--- ...
--- frontend (NEXTJS APP)
--- package.json
--- ...
It worked fine so far, but now when I try to import components in my frontend nextjs application, it gives me a Module not found Error:
./pages/index.js:4:0
Module not found: Can't resolve 'components'
My package.json in the / (root) folder:
{
"name": "root",
"private": true,
"devDependencies": {
"lerna": "^4.0.0"
}
}
package.json in packages/frontend (NextJS App) :
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"components": "0.0.0",
"next": "11.1.2",
"next-transpile-modules": "^8.0.0",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"eslint": "7.32.0",
"eslint-config-next": "11.1.2"
}
}
package.json in the /packages/components folder:
{
"name": "components",
"version": "0.0.0",
"description": "> TODO: description",
"author": "Anton",
"homepage": "",
"license": "ISC",
"main": "dist/index.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1",
"dev": "microbundle watch --jsx React.createElement"
},
"source": "lib/index.js",
"devDependencies": {
"microbundle": "^0.13.3"
}
}
I have been following the linked tutorial pretty much to the dot, I'm really not sure what's going on here, but then again, I am a beginner/low intermediate and would appreciate some help!
Had the same issue following the tutorial here.
The gotcha was that you have to run lerna bootstrap again from the root of the monorepo. For me this fixed it.

Can't resolve 'react-dom' in '/path/node_modules/gatsby-react-router-scroll' when running "gatsby-develop"

Update
It turns out that removing gatsby-plugin-preact from my gatsby-config.js fixes the issue. I don't know why though and I'd like to use preact
Here is the project on Github
I started the project with:
gatsby new gatsby-starter-default https://github.com/gatsbyjs/gatsby-starter-default
I run gatsby develop and get
ERROR #98124 WEBPACK
Generating SSR bundle failed
Can't resolve 'react-dom' in '/path/node_modules/gatsby-react-router-scroll'
If you're trying to use a package make sure that 'react-dom' is installed. If you're trying to use a local file make sure that the path is correct.
File: node_modules/gatsby-react-router-scroll/scroll-container.js
not finished Generating image thumbnails - 7.221s
I've already tried rm -rf node_modules && npm install npm update npm i react-dom npm i react-router-dom and this hasn't changed anything
package.json
{
"name": "gatsby-starter-default",
"private": true,
"description": "A simple starter to get up and developing quickly with Gatsby",
"version": "0.1.0",
"author": "Kyle Mathews <mathews.kyle#gmail.com>",
"dependencies": {
"gatsby": "^2.24.50",
"gatsby-image": "^2.4.16",
"gatsby-plugin-manifest": "^2.4.24",
"gatsby-plugin-netlify": "^2.3.13",
"gatsby-plugin-offline": "^3.2.25",
"gatsby-plugin-preact": "^4.0.9",
"gatsby-plugin-purgecss": "^5.0.0",
"gatsby-plugin-react-helmet": "^3.3.10",
"gatsby-plugin-root-import": "^2.0.5",
"gatsby-plugin-sass": "^2.3.12",
"gatsby-plugin-sharp": "^2.6.28",
"gatsby-plugin-sitemap": "^2.4.12",
"gatsby-plugin-typescript": "^2.4.18",
"gatsby-source-filesystem": "^2.3.26",
"gatsby-transformer-json": "^2.4.11",
"gatsby-transformer-sharp": "^2.5.13",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-bootstrap": "^1.3.0",
"react-dom": "^16.13.1",
"react-helmet": "^6.1.0",
"react-router-dom": "^5.2.0",
"react-scroll": "^1.8.1"
},
"devDependencies": {
"prettier": "2.1.0"
},
"keywords": [
"gatsby"
],
"license": "0BSD",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
gatsby-config.js
const path = require(`path`)
module.exports = {
siteMetadata: {
title: `Title`,
description: `Description.`,
author: `Autho name`,
themeColor: "#d2f5fb",
siteUrl: "https://example.ca",
},
plugins: [
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/assets/images`,
},
},
`gatsby-transformer-json`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `data`,
path: `${__dirname}/src/data`
}
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `gatsby-starter-default`,
short_name: `starter`,
start_url: `/`,
background_color: `#663399`,
theme_color: `#663399`,
display: `minimal-ui`,
icon: `src/assets/images/gatsby-icon.png`, // This path is relative to the root of the site.
},
},
{
resolve: 'gatsby-plugin-root-import',
options: {
"components": path.join(__dirname, "src/components"),
"styles": path.join(__dirname, "src/assets/styles"),
"interfaces": path.join(__dirname, "src/interfaces"),
"data": path.join(__dirname, 'src/data'),
"pages": path.join(__dirname, 'src/pages')
}
},
`gatsby-plugin-typescript`,
`gatsby-plugin-sass`,
`gatsby-plugin-offline`,
`gatsby-plugin-sitemap`,
`gatsby-plugin-netlify`,
`gatsby-plugin-preact`,
{
resolve: `gatsby-plugin-purgecss`,
options: {
// printRejected: true, // Print removed selectors and processed file names
purgeOnly: ['src/assets/styles', 'src/components','node_modules/'],
ignore: ['node_modules/'],
whitelist: [],
whitelistPatterns: []
}
}
],
}
You can have this error when moving from Gatsby v1 to v2. There's an official upgrade guide available here: https://www.gatsbyjs.com/docs/reference/release-notes/migrating-from-v1-to-v2/
The steps I had to do in my case were:
change from gatsby-link to gatsby
install react & react-dom
In your case that would probably updating to the latest version on gatsby-plugin-preact: https://www.gatsbyjs.com/plugins/gatsby-plugin-preact/
Try re-adding gatsby-react-router-scroll.
npm install gatsby-react-router-scroll
You might want to run a gatsby clean first.

getting error : Found bindings for the following environments: - Linux 64-bit with Node.js 9.x in my ubuntu system

when i try to start my project in react, i am getting this error , i am using Ubuntu system
Found bindings for the following environments: - Linux 64-bit with Node.js 9.x
I am using node -v : 10.13.0 and npm -v : 6.11.2, here i have also attached my package.json file, can anyone please help me how to resolve this issue ?
{
"name": "gatsby-starter-hello-world",
"private": true,
"description": "A simplified bare-bones starter for Gatsby",
"version": "0.1.0",
"license": "0BSD",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"dependencies": {
"classnames": "^2.2.6",
"gatsby": "^2.24.48",
"gatsby-background-image": "^1.1.2",
"gatsby-plugin-routes": "^1.0.0",
"gatsby-plugin-sass": "^2.3.12",
"gatsby-plugin-sharp": "^2.6.28",
"gatsby-source-filesystem": "^2.3.25",
"gatsby-transformer-sharp": "^2.5.13",
"node-sass": "^4.14.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.0.1"
},
"devDependencies": {
"prettier": "2.0.5"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-hello-world"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
You just need to use:
npm rebuild node-sass
I would suggest adding a .nvmrc file in the root of your project to avoid this error happen. This is a simple file that fixes the Node version for your project in order to avoid a mismatch between versions and forcing the project to run under a specific version. You just need to add a .nvmrc file with this content:
v10.13.0
Try removing your node_modules and reinstalling. You might also want to check that you don't have something link NVM or and old Node 9 version in your path, that node-sass is picking up while intalling

How do I Create Electron App from existing React Project without disturbing the React Project

I have existing React Apps which are published to production web servers but I want to run them from the desktop using Electron. However, I do not want to disturb the React App package.json. I attempted to decouple an existing online tutorial by paachu on Medium but was unsuccessful. It kept failing trying to locate "main...chunk.css" and other resources needed by the React App.
With that said, I have a working React App in directory ./projx/reactApp I would like to create an Electron App in ./projx/electronApp that will build and create a packaged executable using electron-builder. The dev environment is not necessary at this time in that I only need a release package to distribute. Here is a package.json that almost works:
{
"name": "wrappedReact",
"version": "1.0.0",
"main": "./src/electron.js",
"scripts": {
"preclean": "cross-env rm -rf build",
"react-prebuild": "cross-env cp -r ../reactApp/build build",
"build-tsapp": "tsc ./src/electron.ts",
"electron-prebuild": "mv ./src/*.js ../build",
"electron-build": "npm run electron-prebuild && electron-builder",
"build": "npm run preclean && npm run electron-build",
"startdev": "concurrently \"tsc ./src/electron.ts -w\" \"cross-env
NODE_ENV=dev nodemon --exec \"\"wait-on http://localhost:3000 &&
electron src/electron.js\"\""
},
"author": "sfanjoy",
"homepage": "./",
"electron-pack": "build --em.main=build/electron.js",
"build": {
"appId": "com.example.reactApp",
"productName": "ReactApp",
"copyright": "Copyright © 2019 sfanjoy",
"files": [
"build/**/*",
"node_modules/**/*"
],
"directories": {
"buildResources": "assets"
},
"win": {
"target": "portable",
"icon": "assets/app.ico"
}
},
"dependencies": {
"#types/electron": "^1.6.10"
},
"devDependencies": {
"#types/electron": "^1.6.10",
"concurrently": "^4.1.1",
"cross-env": "^5.2.1",
"electron-builder": "^21.2.0",
"electron-is-dev": "^1.1.0",
"nodemon": "^1.19.1",
"typescript": "^3.5.2",
"wait-on": "^3.2.0"
}
}
I think the solution is with electron-builder if it would allow you to inject the ./src/electron.js into the prebuilt reactApp\dist\win-unpacked\resources\app.asar. I could then copy the reactApp/build directory, inject into the asar, and call electron-builder.
This by far (IMO) is the best starting setup (Boilerplate) I have come accross:
https://github.com/nateshmbhat/electron-react-ts-starter/

How do I run my mocha.js tests in visual studio online build process

I have a react web application, that uses webpack.
Due to the issue with Left-pad I am including my node_modules folder in source control.
I have mocha and enzyme set up to unit test my react modules and my packages.json has the following test configuration.
"test": "mocha-webpack --webpack-config webpack.config-test.js \"source/**/*.test.js\""
It all works great. when I run
npm run test
on my local development machine. All my test's get executed.
The problem I have is when I run "npm run test" as part of a build process on a dedicated build machine I get the following error
Error: Cannot find module 'enzyme'
After some digging around, I discovered that if I ran
npm install enzyme
on the build machine before I ran the test, then it worked fine.
The reason this worked is that the install modified my node_modules/enzyme/packages.json file. There are 2 full paths stored in this file that had the paths to my local dev environment in them, and they got changed to the full path to where my project was run on the build machine.
One under _Args and one under _where
{
"_args": [
[
{
"raw": "enzyme#^2.6.0",
"scope": null,
"escapedName": "enzyme",
"name": "enzyme",
"rawSpec": "^2.6.0",
"spec": ">=2.6.0 <3.0.0",
"type": "range"
},
"C:\\Dev\\UnifiedWeb"
]
],
"_from": "enzyme#>=2.6.0 <3.0.0",
"_id": "enzyme#2.6.0",
"_inCache": true,
"_location": "/enzyme",
"_nodeVersion": "7.0.0",
"_npmOperationalInternal": {
"host": "packages-12-west.internal.npmjs.com",
"tmp": "tmp/enzyme-2.6.0.tgz_1478746866117_0.454174768878147"
},
"_npmUser": {
"name": "ljharb",
"email": "ljharb#gmail.com"
},
"_npmVersion": "3.10.8",
"_phantomChildren": {},
"_requested": {
"raw": "enzyme#^2.6.0",
"scope": null,
"escapedName": "enzyme",
"name": "enzyme",
"rawSpec": "^2.6.0",
"spec": ">=2.6.0 <3.0.0",
"type": "range"
},
"_requiredBy": [
"#DEV:/",
"#USER"
],
"_resolved": "https://registry.npmjs.org/enzyme/-/enzyme-2.6.0.tgz",
"_shasum": "148d742b25e2565f7e80870a0c92aea9be1b90ea",
"_shrinkwrap": null,
"_spec": "enzyme#^2.6.0",
"_where": "C:\\Dev\\UnifiedWeb",
"author": {
"name": "Leland Richardson",
"email": "leland.richardson#airbnb.com"
},
"bugs": {
"url": "https://github.com/airbnb/enzyme/issues"
},
"dependencies": {
"cheerio": "^0.22.0",
"function.prototype.name": "^1.0.0",
"in-publish": "^2.0.0",
"is-subset": "^0.1.1",
"lodash": "^4.16.4",
"object-is": "^1.0.1",
"object.assign": "^4.0.4",
"object.entries": "^1.0.3",
"object.values": "^1.0.3",
"uuid": "^2.0.3"
},
"description": "JavaScript Testing utilities for React",
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-eslint": "^7.1.0",
"babel-loader": "^6.2.7",
"babel-preset-airbnb": "^2.1.1",
"babel-register": "^6.18.0",
"chai": "^3.5.0",
"coveralls": "^2.11.14",
"enzyme-example-jest": "^0.1.0",
"enzyme-example-karma": "^0.1.1",
"enzyme-example-karma-webpack": "^0.1.4",
"enzyme-example-mocha": "^0.1.0",
"enzyme-example-react-native": "^0.1.0",
"eslint": "^3.9.1",
"eslint-config-airbnb": "^13.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.6.0",
"gitbook-cli": "^1.0.1",
"istanbul": "^1.0.0-alpha.2",
"jsdom": "^6.1.0",
"json-loader": "^0.5.4",
"karma": "^1.3.0",
"karma-chrome-launcher": "^1.0.1",
"karma-firefox-launcher": "^1.0.0",
"karma-mocha": "^1.2.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.8.0",
"mocha": "^3.1.2",
"rimraf": "^2.5.4",
"safe-publish-latest": "^1.1.1",
"sinon": "^1.17.6",
"webpack": "^1.13.3"
},
"directories": {},
"dist": {
"shasum": "148d742b25e2565f7e80870a0c92aea9be1b90ea",
"tarball": "https://registry.npmjs.org/enzyme/-/enzyme-2.6.0.tgz"
},
"gitHead": "5c37d91715a88d393f3c260447a189a446d76e0c",
"homepage": "https://github.com/airbnb/enzyme#readme",
"keywords": [
"javascript",
"shallow rendering",
"shallowRender",
"test",
"reactjs",
"react",
"flux",
"testing",
"test utils",
"assertion helpers",
"tdd",
"mocha"
],
"license": "MIT",
"main": "build",
"maintainers": [
{
"name": "airbnb",
"email": "jordan.harband+npm#airbnb.com"
},
{
"name": "intelligibabble",
"email": "leland.m.richardson#gmail.com"
},
{
"name": "ljharb",
"email": "ljharb#gmail.com"
}
],
"name": "enzyme",
"optionalDependencies": {},
"peerDependencies": {
"react": "0.13.x || 0.14.x || ^15.0.0-0 || 15.x"
},
"readme": "ERROR: No README data found!",
"repository": {
"type": "git",
"url": "git+https://github.com/airbnb/enzyme.git"
},
"scripts": {
"build": "babel src --out-dir build",
"check": "npm run lint && npm run test:all",
"clean": "rimraf build",
"docs:build": "npm run docs:prepare && gitbook build",
"docs:clean": "rimraf _book",
"docs:prepare": "gitbook install",
"docs:publish": "npm run docs:clean && npm run docs:build && cd _book && git init && git commit --allow-empty -m 'update book' && git fetch git#github.com:airbnb/enzyme.git gh-pages && git checkout -b gh-pages && git add . && git commit -am 'update book' && git push git#github.com:airbnb/enzyme.git gh-pages --force",
"docs:watch": "npm run docs:prepare && gitbook serve",
"lint": "eslint --ext js,jsx src test",
"postversion": "git push && git push --tags && npm run clean && npm run docs:publish",
"prepublish": "not-in-publish || (npm run clean && npm run build && safe-publish-latest)",
"pretest": "npm run lint",
"preversion": "npm run clean && npm run check",
"react:13": "npm run react:clean && npm i react#0.13",
"react:14": "npm run react:clean && npm i react#0.14 react-dom#0.14 react-addons-test-utils#0.14",
"react:15": "npm run react:clean && npm i react#15 react-dom#15 react-addons-test-utils#15",
"react:clean": "rimraf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils",
"test": "npm run clean && npm run build && npm run test:only",
"test:all": "npm run react:13 && npm run test:only && npm run react:14 && npm run test:only && npm run react:15 && npm run test:only",
"test:env": "sh ./example-test.sh",
"test:karma": "karma start",
"test:only": "mocha --recursive test",
"test:single": "mocha --watch",
"test:watch": "mocha --recursive --watch test",
"travis": "babel-node ./node_modules/.bin/istanbul cover --report html _mocha -- test --recursive",
"version": "npm run build"
},
"version": "2.6.0"
}
This problem will also affect other developers in my team, as the paths will need to be changed to their local development if they want to run the tests locally.
Why is the full path in the enzyme package? Is there a better way of handling this? Am I missing something, or is npm not portable?
Have you tried npm link ext/* --prefix=node_modules
As per this thread - Local dependency in package.json

Resources