I am working on a website using Preact, and I am trying to proxy requests made in the browser to the development server I'm using as the back end.
When I try to run the front end application with yarn client, it errors out and informs me it cannot find the http2-Proxy module despite my installing it.
My directory structure is as follows:
Website_Root:
| package.json
|
+---dummy-api
|
+---frontend
| package.json
| README.md
| snowpack.config.mjs
|
+---public
|
+---src
| index.jsx
|
+---assets
|
+---components
|
+---routes
I'm attempting to use the Yarn Workspaces feature; as such my package.json at the root level is as follows:
{
"name": "Website",
"packageManager": "yarn#3.1.1",
"scripts": {
"client": "yarn workspace frontend dev",
"mockapi": "yarn workspace dummy-api dev",
"dev": "yarn concurrently --kill-others-on-fail \"yarn client\" \"yarn mockapi\""
},
"workspaces": [
"frontend",
"dummy-api"
],
"devDependencies": {
"concurrently": "^6.4.0"
},
"dependencies": {
"preact": "^10.6.4"
}
}
The package.json inside of the frontend workspace is as follows:
{
"name": "frontend",
"private": true,
"proxy": "http://localhost:9000/api",
"scripts": {
"dev": "snowpack dev",
"build": "snowpack build",
},
"dependencies": {
"axios": "^0.24.0",
"http2-proxy": "^5.0.53",
"preact": "^10.6.4",
"preact-router": "^3.2.1",
"react-table": "^7.7.0"
},
"devDependencies": {
"#prefresh/snowpack": "^3.0.0",
"#snowpack/plugin-dotenv": "^2.1.0",
"#snowpack/web-test-runner-plugin": "^0.2.2",
"snowpack": "^3.8.8"
}
}
And, finally, my snowpack.config.js is as follows:
import proxy from "http2-proxy";
/** #type {import("snowpack").SnowpackUserConfig } */
export default {
alias: {
react: "preact/compat",
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
"react/jsx-runtime": "preact/jsx-runtime",
},
mount: {
public: { url: "/", static: true },
src: { url: "/dist" },
},
plugins: ["#snowpack/plugin-dotenv", "#prefresh/snowpack"],
routes: [
{
match: "all",
src: "/api/.*",
dest: (req, res) =>
proxy.web(req, res, { hostname: "localhost", port: "9000" }),
},
],
devOptions: {
open: "none",
},
};
I can't help but think I've set something up incorrectly; snowpack should be able to find http2-proxy since I've installed it, but I genuinely don't understand what the holdup is.
I've also tried installing http2-proxy at the root level to no avail.
I believe I discovered the problem.
I did not initialize the yarn workspaces correctly.
I tried out a minimum working example of preact with snowpack, and found that creating a parent yarn directory, creating the child directories, registering them in the parent package.json and then inside each child subdirectory running yarn init -y instead of yarn init -2 produced the necessary results.
Related
I am experimenting Monorepo using yarn workspaces method containing a sample react-native project which is configured and it works fine in debug build. When I created a release build and run the app, it crashes as soon as it opens. I have provided the log and the code below.
Android Studio Logcat:
2022-05-16 23:18:08.466 6620-6644/? E/AndroidRuntime: FATAL EXCEPTION: create_react_context
Process: com.sample.mobile, PID: 6620
java.lang.RuntimeException: Unable to load script. Make sure you're either running Metro (run 'npx react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.
at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:248)
at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:29)
at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:277)
at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1422)
at com.facebook.react.ReactInstanceManager.access$1200(ReactInstanceManager.java:138)
at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:1111)
at java.lang.Thread.run(Thread.java:923)
Monorepo structure:
monorepo
|
|___Common
|
|___Mobile
Monorepo's root package.json:
{
"name": "monorepo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "UNLICENSED",
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/react-native",
"**/react-native/**"
]
},
"references": [
{
"path": "packages/Common"
},
{
"path": "packages/Mobile"
}
],
"private": true
}
Common's root package.json
{
"name": "common",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "UNLICENSED",
"devDependencies": {
"#types/react": "^18.0.9",
"#types/react-native": "^0.67.7"
}
}
React Native's package.json:
...
"peerDependencies": {
"common": "0.0.1"
},
...
React Native's metro.config.js
const path = require('path');
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
resolver: {
sourceExts: ['jsx', 'js', 'ts', 'tsx', 'mjs'],
extraNodeModules: new Proxy(
{},
{
get: (target, name) => {
return path.join(__dirname, `node_modules/${name}`);
},
},
),
},
watchFolders: [
path.resolve(__dirname),
path.resolve(__dirname.replace('Mobile', 'Common'))
],
};
Any Suggestions?
I figured out that the issue was not on the monorepo setup but React Native itself. I am using the latest version of React Native (0.68.2) which was causing this issue. I verified this my creating a simple fresh React Native Project in which this same issue occurs. There are also issues open in the React Native repo itself.
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.
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.
I made a very simple gatsby site with AWS Amplify tho, site map was not created by 'gatsby-plugin-sitemap'.
[package.json]
{
"name": "web_matching",
"version": "1.0.0",
"private": true,
"description": "web_matching",
"author": "uekyo",
"keywords": [
"gatsby"
],
"scripts": {
"develop": "gatsby develop",
"start": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve",
"clean": "gatsby clean"
},
"dependencies": {
"gatsby": "^2.26.1",
"gatsby-plugin-sitemap": "^2.11.0",
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
}
[gatsby-config.js]
module.exports = {
siteMetadata: {
title: "web_matching",
siteUrl: `https://amp.d1aw1cuurv9iud.amplifyapp.com`,
},
plugins: [
{
resolve: `gatsby-plugin-sitemap`,
options: {
output: `/sitemap.xml`,
}
}
]
};
[amplify.yml]
version: 1
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn build
artifacts:
baseDirectory: public
files:
- '**/*'
cache:
paths:
- node_modules/**/*
Do you guys have the same experience? By the way, if I enter the sitemap URL, it's moved to a top page automatically. However, it works on localhost. The problem seems to be in Amplify.
Running locally with gatsby develop will not serve up the sitemap.
You must run under production mode by running gatsby build && gatsby serve
Then the sitemap should be available via http://localhost:9000/sitemap.xml
What changes do I have to make inorder to actually run a successful build and host my website on bluemix?
Right now, this is my Gruntfile.js, manifest.yml and package.json. The base folder 'app' is an Angular application -
module.exports = function(grunt){
grunt.initConfig({
connect: {
server: {
options: {
port: 9000,
base: 'app',
keepalive: true,
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.registerTask('default', ['connect']);
};
package.json:
{
"name": "dummy",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {"grunt-cli": "^1.2.0",
"grunt": "^0.4.5",
"grunt-contrib-connect": "^0.10.1"
}
}
manifest.yml:
---
applications: #Reference http://docs.cloudfoundry.com/docs/using/deploying-apps/manifest.html
- name: dummy #Application Name. Unique to the user's Space
memory: 256M #The maximum memory to allocate to each application instance
instances: 1 #The number of instances of the application to start
path: ./ #Path to the application to be pushed
command: npm install && node_modules/.bin/grunt serve #The command to use to start the application
Few suggestions when working with node applications and Bluemix:
To start a node application it's recommended to use npm start and specify the script in the package.json.
If your application needs to build assets, specify how to do that in the postinstall script in the package.json. For example if you are using Gulp and your main task is build you will have something like:
"postinstall": "gulp build"
When running in Bluemix VCAP_APP_HOST and VCAP_APP_PORT will have the host and port where your application will run.
The files below have the fixes mentioned above:
manifest.yml:
---
applications:
- name: dummy-kartik-yadav
memory: 512M
instances: 1
path: .
command: npm start
package.json:
{
"name": "dummy",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node_modules/.bin/grunt serve",
"postinstall": "console.log('build ui assets like js, css, html...')"
},
"author": "",
"license": "ISC",
"dependencies": {
"grunt-cli": "^1.2.0",
"grunt": "^0.4.5",
"grunt-contrib-connect": "^0.10.1"
}
}
gruntfile.js:
module.exports = function(grunt){
grunt.initConfig({
connect: {
server: {
options: {
port: process.env.VCAP_APP_PORT || 9000, # listen to the port that bluemix will assign you
base: 'app',
keepalive: true
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.registerTask('default', ['connect']);
};
After doing the changes mentioned above, open the project folder and run npm start. If it works locally, it will in Bluemix.