I started a new Laravel project and now Laravel Mix has been replaced with Vite.
I've installed Alpine.js and launched it in the bootstrap.js file but, Alpine.js not recognized in the Laravel blade files and the other JS files.
vite.config
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import path from "path";
export default defineConfig({
plugins: [
laravel([
'resources/css/app.css',
'resources/js/app.js',
]),
],
resolve:{
alias:{
'~alpine':path.resolve(__dirname,'node_modules/alpinejs'),
}
}
});
enter image description here
app.js
import './bootstrap';
import alpine from "./alpinejs/src/alpine";
console.log(alpine.version);
enter image description here
I add Alpine like the following.
bootstrap.js
import _ from 'lodash';
window._ = _;
import $ from 'jquery';
window.jQuery = window.$ = $
import Alpine from 'alpinejs';
window.Alpine = Alpine;
Alpine.start();
I downloaded a template and put all the CSS in a folder and imported it into the App.js file, and now I want to import the JavaScript files, but it gives an error, I even used / * global jQuery * / and put it in the index.js file. but it still gives an error
Is it possible to import JS file at all? If so, thank you for your reply
Error image:Error Text
UPDATE
in App.js
import './App.css';
import './dist/css/adminlte.min.css';
import './dist/css/bootstrap-rtl.min.css';
import './dist/css/custom-style.css';
import './plugins/font-awesome/css/font-awesome.min.css';
import 'jquery';
import jQuery from 'jquery';
import $ from 'jquery';
import './dist/js/adminlte.js';
You can generally just import your JS Files like any other React Component.
JS: export function myfunc(){return "Your JS stuff here")
React: import {myfunc} from "wherever"
I'm currently trying to load a file polyfills.js when the browser is IE.
The file has a bunch of import statements for the polyfills.
import 'core-js/features/array/for-each';
import 'core-js/features/array/find';
import 'core-js/features/array/flat';
import 'core-js/features/array/flat-map';
import 'core-js/features/array/includes';
import 'core-js/features/array/map';
import 'core-js/features/object/assign';
import 'core-js/features/object/from-entries';
import 'core-js/features/object/entries';
import 'core-js/features/string/includes';
import 'core-js/features/url-search-params';
import 'core-js/features/object/keys';
import 'core-js/features/object/values';
import 'core-js/features/object/is-extensible';
// Fetch
import 'whatwg-fetch';
// ACCESS Patterns Polyfills
import 'utilities/element/matches';
import 'utilities/element/closest';
import 'utilities/element/remove';
import 'utilities/nodelist/foreach';
import 'modules/polyfill-replace-with';
I have a function that triggers when the browser is IE and it loads the compiled file polyfills.js via a script tag.
(() => {
if (/*#cc_on!#*/false || !!document["documentMode"]) {
let fileref = document.createElement('script');
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", 'assets/js/polyfill.955d1924.js');
document.getElementsByTagName("head")[0].appendChild(fileref)
console.log('IE DIE!')
}
else {
return false;
}
})();
However, although the file is loaded via the script tag as shown in the image below:
The polyfills still seem to not be working on IE. Is there a fix?
I've a Rails app with webpacker for js & css
I have my admin.js file
import Rails from 'rails-ujs';
import Turbolinks from 'turbolinks';
import 'jquery';
Rails.start();
Turbolinks.start();
import '../admin/styles/app.scss';
// load all images
import '../admin/images/init.js.erb';
// load all fonts
import 'typeface-roboto';
import 'typeface-poppins';
// Theme Vendors
import 'bootstrap/dist/js/bootstrap';
import 'sticky-js/dist/sticky.min';
// init global theme javascript
import '../src/admin/framework/lib/util.js';
import '../src/admin/framework/lib/app.js';
// init global user javascript
import '../admin/global/init';
// init page specific javascript
import '../admin/page_specific/admin_home_index';
import '../admin/page_specific/admin_users_index';
import '../admin/page_specific/admin_products_index';
I need to load Sticky-js before the app init, but in console I have
app.js:130 Uncaught ReferenceError: Sticky is not defined
at _initSticky (app.js:130)
If I open the javascript compiled by webpacker my Sticky JS vendor script is at the end of the file, and I cannot understand why. I can move it on every position but is is always at the end.
Why?
I have the same error as this answer, except instead of it just occurring in one file it is occurring in many; once I fix it for one file, another just pops up with the same error. I've seen this answer but whenever I run react-scripts start a node_modules folder is created in my src, so that solution isn't viable.
It would be too time consuming to have to fix every file that has this error every time I compile, so how can I get rid of this error? It seems to just be an eslint issue.
you will get this error if you declare variable in between your imports,
import React from 'react';
import axios from 'axios';
const URL = process.env.REACT_APP_API_BASE;
import demoXLXSFile from '../../assets/others/Demo.xlsx';
import './student.list.styles.scss';
declare variables after importing everything,
import React from 'react';
import axios from 'axios';
import demoXLXSFile from '../../assets/others/Demo.xlsx';
import './student.list.styles.scss';
const URL = process.env.REACT_APP_API_BASE;
I found this issue while I was using React.lazy in my existing project. I got this error Failed to compile. :- Import in body of module; reorder to top import/first (with create-react-app).
import React from 'react';
import SelectField from '../inputs/SelectField';
const Questions = React.lazy(() => import('./questions'))
import _ from 'lodash';
Solution:-
Only reorder the position i.e. put all import on top then react.lazy.
import React from 'react';
import SelectField from '../inputs/SelectField';
import _ from 'lodash';
const Questions = React.lazy(() => import('./questions'))
I got this same error when I added an extra semicolon ';' at the end of an import statement.
I suggest removing any extraneous semicolons. This should make the error go away.
Moving every import statement to the top of the file solves the issue.
Happened to me when I put require before import like this:
require('dotenv').config()
import React from 'react';
import ReactDOM from 'react-dom';
...
Solution: Put the require after the imports
If You are using Component Lazy loading then always put lazy load component import code below normal import code.
Correct Example
import First from './first'
const Second = React.lazy(()=>import("./Second))
Incorrect Example
const Second = React.lazy(()=>import("./Second))
import First from './first'
I came across this issue too. I found that you must import all ES6 modules at the top level of your JavaScript files."... the structure of ES6 modules is static, you can’t conditionally import or export things. That brings a variety of benefits.
This restriction is enforced syntactically by only allowing imports and exports at the top level of a module:"
From Dr. Axel Rauschmayer’s Exploring JS:
Wrong
1.
import React ,{useState ,useEffect} from 'react';
import './App.css';
import Post from './Post';
import db from "./firebase"
Right
2.
import React ,{useState ,useEffect} from 'react';
import './App.css';
import Post from './Post';
import db from "./firebase.js"
//this is code firebase.js
const db = firebaseApp.firestore();
const auth = firebase.auth();
const storage = firebase.storage();
export default {db,auth,storage};
When I change the firebase into firebase.js in Snippet 2.
My Error vanished
I had forgotten to add from.
Before:
import UpperBlock;
After:
import UpperBlock from "../components/dashboard/shared/UpperBlock";
Make sure you import well your component and then stop the server and restart it again. It worked for me.
I forgot to add {Component} after importing 'react' library to my project, this solved my issue.
Move all of your imports to the top of the file.
Even in case of a require (that is written in between import statements), this error will come.
e.g.
import 'some_module';
require('some_file');
import 'some_other_module');
This would result in an error.
You would want to do this instead:
import 'some_module';
import 'some_other_module');
require('some_file');
I was facing the same issue when I installed chat.js library in my reactJs project. I solved this issue by moving my chart.Js import to the index.js file
index.js file:
import React from "react";
import ReactDOM from "react-dom";
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js"; <--- imported
import "./index.css";
import App from "./App";
ChartJS.register(ArcElement, Tooltip, Legend); <---- imported
If you're experiencing this error in modern versions of react(current version 18.0.0) make sure you're making all your imports before the ReactDOM.createRoot declaration.
For example, I got the error with:
import App from "./App";
import reportWebVitals from "./reportWebVitals";
const root = ReactDOM.createRoot(document.getElementById("root"));
import { Provider } from "./context";
This will result in an error. Instead, import everything before the createRoot:
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Provider } from "./context";
const root = ReactDOM.createRoot(document.getElementById("root"));
This will fix the error. Simple fix but it's easy to miss
If I put double Semicolon behind the importing statement than I got "error".you can see difference between two pictures in import './index.css'; is different
For Example :-
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';;
import 'tachyons';
import {robots} from "./Robots";
import reportWebVitals from './reportWebVitals';
import CardList from './CardList';