Dynamic import not working in netlify (reactjs) - javascript

I'm trying to deploy my reactjs application which uses #babel/plugin-syntax-dynamic-import, the application works fine on my localhost, but when i deploy it on netlify, only the root url works (ex: mysite.netlify.com), all other url's with slug (ex: mysite.netlify.com/Visit) doesn't work, it simply returns 404 page not found even though i have defined routes and working on my localhost.
i have the following settings in my .babelrc file
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": ["#babel/plugin-syntax-dynamic-import"]
}
and in my package.json
"dependencies": {
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
...
}
and i have the following imports on my App.js
import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
const Photography = lazy(() => import('../Form1/Main.js'));
const Visit = lazy(() => import('../Form2/Main.js'));
is there anything missing or i need to make it work on netlify?

I don't think this is an issue with dynamic imports - this is an issue with your Netlify config. Add a _redirects file to the public root of your project. With the following content:
/* /index.html 200
More info here
An alternative is to add a netlify.toml file but this is more complex and will achieve the same result. More info here

Related

How Do I setup a backend such as node.js/express with react(using vite) also how do I deploy, do I use vite's commands or node's

(LOOK AT TITLE) I am a beginner in react and I wanted to use Vite.JS I know nothing about Node.js but I want to learn it so I need to connect them. Please let me know! also how do I deploy, should I use vite's commands As I would do without a backend or are there special commands, should I be in the backend folder When deploying? That's all if you know any answers please Hit me up!
For backend configuration, from the official vite documentaion;
In your Vite config.js file,overwrite its content with the following:
import { defineConfig } from 'vite'
import react from '#vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
build: {
manifest: true,
rollupOptions: {
input: './src/main.jsx',
},
},
server: {
proxy: {
"/api": "http://localhost:5000/", // the address that u serve in the backend
},
},
})
This is to set the bundle’s entry point to src/main.jsx instead of the default index.html.
Inside your public folder, there is the index.html, or it might be inside the src directory depending with your vite-js version. Edit its contents to point the main.jsx file path.
<script type="module" src="/src/main.jsx"></script>
That's it.

Next.js imports from root directory: is it valid to start an import with a slash?

This import works, but I can't find any documentation saying that it's valid:
// rather than do a crazy nested import like this
import { myUtil } from '../../../../../lib/utils'
// this appears to work just fine
import { myUtil } from '/lib/utils'
I see where the official answer is to set up a config file with a # alias, so I suppose I will just do that to be compliant. Just thought it was curious that this import worked just fine. I am using the default configuration for a Next.js project set up with create-next-app
Not sure if this is your case, but if the top folder is in the root directory, you can use this
import { myUtil } from './lib/utils'
I'm guessing that's what's happening, even though you don't have the period in yours.

Import more than one class from node_modules

I'm currently developing a package, and inside my package repository I have the following structure:
src
- Requests.js
- Constants.js
package.json
Inside my package.json, I have the following:
{
"name": "package-name",
"version": "1.0.0",
"main": "src/Requests"
}
Then inside my project, I do the following to retrieve my module:
import Requests from 'package-name';
Now what I'm trying to achieve is to import the Constants class from the package. However, when I do the following, I get a compiled error that it cannot locate the class.
import Constants from 'package-name/Constants';
To get it working, I have to do but I don't want to have the /src in the import path.
import Constants from 'package-name/src/Constants';
I've tried to change the main in my package.json to the directory, but it's still not working:
"main": "src/"
Right, this is not a bug, just how node imports are handled by your bundler. There are many solutions to this problem, here are a couple:
The most user friendly is to have your publish command copy those components to the root directory and then publish. That way you'll be able to import like you say, import Constants from 'package-name/Constants'. It's only slightly inconvenient for you as a developer but a script should be able to clean up after the publish command succeeds.
An alternate solution is to have a third file, maybe call it index.js that looks somewhat like this:
import Requests from './Requests';
import Constants from './Constants';
export default {
Requests,
Constants,
};
This will allow you to import like this (don't forget to change your package.json to have "main": "src/index.js"):
import { Requests, Constants } from 'package-name';
The only reason I would shy away from this approach is that when this import statement is handled by the bundler, ALL components are imported, even if you only need one of them. It could make your bundle larger if your library contains many different components.

Node.js + Vue.js: why is console.log() in vue.js files not showing in console?

I'm new to javascript development and I have a real project here. Let me describe:
There is a project, an Express (Node.js) server that has a /public/app folder
There is another project, a Vue.js app that has a /dist folder
In the Express /public/app folder is copypasted vue.js application (copied from another project from /dist folder)
Vue.js app runs at http://localhost:3000/app/#/
I've added some console.log() commands into a different files/places in a vue.js app code, for example:
app.ts
...
import {store} from './store/store';
import {isBoolean} from 'util';
console.log('APP');
let router = new VueRouter({
routes: [
{ path: '/', component: WelcomeComponent },
...
or in component:
...
import * as common from '../../../store/common';
import * as country from '../../../store/country';
console.log('COMPONENT');
#Component({
template: require('./template.html'),
components: {
'layout': LayoutContent2,
...
and so on. But none of the console.log() messages are displayed in a browser console. Im sure that an app is builded and copied correctly. So why can't I see the messages in console?
The problem was, that Windows was not replacing the old main.min.js file with the new one. After manually removing and copying the files again it works!

Can't resolve module (not found) in React.js

I can't believe that I'm asking an obvious question, but I still get the error in console log.
Console says that it can't find the module in the directory, but I've checked at least 10 times for typos. Anyways, here's the component code.
I want to render Header in root
import React, { Component } from 'react'
import Header from './src/components/header/header'
import logo from './logo.svg'
import './App.css'
class App extends Component {
render() {
return (
<Header/>
);
}
}
export default App;
This is the Header component
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import navBar from './src/components/header/navBar'
import './src/css/header.css'
class Header extends Component {
render() {
return {
<div>
<div id="particles-js"></div>
<navBar/>
<Title/>
</div>
};
}
}
ReactDOM.render(<Header/>, document.getElementById('header'));
I've checked at least 10 times that the module is at this location ./src/components/header/header, and it is (folder "header" contains "header.js").
Yet, React still throws this error:
Failed to compile
./src/App.js
Module not found: Can't resolve './src/components/header/header' in '/home/wiseman/Desktop/React_Components/github-portfolio/src'
npm test says the same thing.
The way we usually use import is based on relative path.
. and .. are similar to how we use to navigate in terminal like cd .. to go out of directory and mv ~/file . to move a file to current directory.
my-app/
node_modules/
package.json
src/
containers/card.js
components/header.js
App.js
index.js
In your case, App.js is in src/ directory while header.js is in src/components. To import you would do import Header from './components/header'. This roughly translate to in my current directory, find the components folder that contain a header file.
Now, if from header.js, you need to import something from card, you would do this. import Card from '../containers/card'. This translate to, move out of my current directory, look for a folder name containers that have a card file.
As for import React, { Component } from 'react', this does not start with a ./ or ../ or / therefore node will start looking for the module in the node_modules in a specific order till react is found. For a more detail understanding, it can be read here.
If you create an application with react-create-app, don't forget set environment variable:
NODE_PATH=./src
Or add to .env file to your root folder;
Deleted the package-lock.json file & then ran
npm install
Read further
in my case, The error message was
Module not found: Error: Can't resolve '/components/body
While everything was in the correct directory.
I found that renaming body.jsx to body.js resolve the issue!
So I added this code in webpack.config.js to resolve jsx as js
module.exports = {
//...
resolve: {
extensions: ['.js', '.jsx']
}
};
And then build error gone!
Adding NODE_PATH as environment variable in .env is deprecated and is replaced by adding "baseUrl": "./src", to compilerOptions in jsconfig.json or tsconfig.json.
Reference
I think its the double use of header. I just tried something similar myself and also caused issues. I capitalized my component file to match the others and it worked.
import Header from './src/components/header/header';
Should be
import Header from './src/components/header/Header';
There is a better way you can handle the import of modules in your React App.
Consider doing this:
Add a jsconfig.json file to your base folder. That is the same folder containing your package.json. Next define your base URL imports in it:
//jsconfig.json
{
"compilerOptions": {
"baseUrl": "./src"
}
}
Now rather than calling ../../ you can easily do this instead:
import navBar from 'components/header/navBar'
import 'css/header.css'
Notice that 'components/' is different from '../components/'
It's neater this way.
But if you want to import files in the same directory you can do this also:
import logo from './logo.svg'
I solved by putting the file extension
import MyComponent from "src/components/MyComponent";
to
import MyComponent from "src/components/MyComponent.tsx";
I had a similar issue.
Cause:
import HomeComponent from "components/HomeComponent";
Solution:
import HomeComponent from "./components/HomeComponent";
NOTE: ./ was before components. You can read #Zac Kwan's post above on how to use import
You can try to execute 'npm install' in the app folder. This might also solve the problem. It worked for me.
I faced the same issue when I created a new react app, I tried all options in https://github.com/facebook/create-react-app/issues/2534 but it didn't help. I had to change the port for the new app and then it worked. By default, apps use the port 3000.I changed the port to 8001 in package.json as follows:
"scripts": {
"start": "PORT=8001 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
If you are using create-react-app, and have just added typescript to it, check whether a tsconfig.json has been auto-generated for you. The CRA docs say that it should be, but there seems to be a bug at the moment where it is not being generated.
If the tsconfig.json is missing, there's a few ways to create one yourself.
Copy one off the internet or from another repo
npx tsc --init
Create a fresh project somewhere else using npx create-react-app my-ts-proj --template typescript and then copy the tsconfig over from there
you should change import Header from './src/components/header/header' to
import Header from '../src/components/header/header'
You need to be in project folder, if you are in src or public you have to come out of those folders. Suppose your react-project name is 'hello-react' then cd hello-react
I was facing the same problem and I resolved it.
See if your index.js file is in src folder, then what ever file you are importing, the folder containing that must also be inside the src folder.
That means if your components folder is outside the src folder, just drag it inside the src folder in your editor because the files outside of src folder are not imported.
Then you shall be able to import using ./components/header/header(in this case)
For me, I had the input correct but npm start can be buggy (at least using it with Hyper terminal on Windows and Linux). If I move files to different folders, npm start doesn't pick up on these changes. I need to cancel npm start process, make the move, save and then run npm start and it will see the files now.
replace ReactDOM.render(<Header/>, document.getElementById('header')); by export default Header in Header.js
I just had this issue from auto-importing a component, no type or webpack config issues.
What fixed it was changing the import from relative to the app root directory to relative to the file:
import MyComponent from "src/components/MyComponent";
to
import MyComponent from "../components/MyComponent";
If you're getting this from Visual Studio Code auto-importing via the shortest route, you can change it so it imports relatively. By going here:
menu File → Preferences → Settings → User Settings,
"typescript.preferences.importModuleSpecifier": "relative"
It is working for me just (./) no need src here
import Header from './components/header/header'
You might be importing .tsx file inside a .js file. Ensure that if you are working on a javascript source, you have extensions .js or .jsx not .tsx :)
In my case I rename a component file, an VS Code add the below line of code for me:
import React, { Component } from "./node_modules/react";
So I fixed by removing the: ./node_modules/
import React, { Component } from "react";
Cheers!
I think it may help you-
Read your error carefully-./src/App.js
Module not found: Can't resolve './src/components/header/header' in '/home/wiseman/Desktop/React_Components/github-portfolio/src'
just write- ./header/header instead ./src/components/header/header in App.js
if it doesnt work try to change header file name may be head
Check for the import statements.It should be ended with semicolon. If you miss any, you will get this error.
Also check whether following import statement added in you component.
import { threadId } from 'worker_threads';
If so remove that line. It works for me.

Categories