Error trying to import 'systemjs' to use import dynamically - javascript

I am trying to use SystemJS to import a module that lives on an external server. Having problems just getting started with importing systemjs so that I can use it in code.
import System from 'systemjs';
OR
import * as System from 'systemjs';
// System not found, error on line above trying to import systemjs as module.
System.import('https://test.com/modules/somemodule.js').then(module => {
// got the module
});
Neither work. This is all assuming its possible to use System.import(...) in code to import a module. Reading through the docs not really sure how to import systemjs.
Error:
Module not found: Error: Can't resolve 'systemjs'

Related

How to import Papaparse

I have a Svelte/Js/Vite app and need to import Papaparse. It's installed as npm install papaparse and is present in the package.json.
When I import it as import * as Papa from "papaparse";, methods are not visible in the IDE (i.e. parse()) and the following error is shown:
error loading dynamically imported module. This could be due to syntax errors or importing non-existent modules. (see errors above)
When I import it as import {Papa} from "papaparse";, methods are visible in the IDE, but there s a message
Cannot resolve symbol 'Papa'
and it anyway doesn't work in a browser with the same error.
What is the proper way to import and what is the reason for these problems.
I think you need to import it this way since it's a default exported member:
import Papa from "papaparse";
// the name does not really matter. You can name it whatever you want
// as you're basically saying:
// import { default as Papa } from "papaparse"
// So, you can do it like this too:
// import Mama from "papaparse";
If you'd like to learn more about why, I'd recommend reading this article:
https://www.digitalocean.com/community/tutorials/understanding-modules-and-import-and-export-statements-in-javascript

The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script

I Just created a react app. The npm start seems to work fine but npm run build is constantly failing. I need to run npm run build to deploy it on some website.
Already gone through all posts related to this on stackoverflow.com. But didn't found any working solution.
import './App.css';
import 'https://kit.fontawesome.com/a076d05399.js'
import Navbar from './components/navbar';
import Homepage from './components/homepage';
import Skills from './components/Skills';
import Project from './components/project';
import Contact from './components/contact';
Error Message
Failed to compile.
The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script
The problem here was the import statement of an external js file.
import 'https://kit.fontawesome.com/a076d05399.js';
You can add the file in index.html & run build.
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
And yes there's no problem with import statement in css.
#import url("https://cdn.jsdelivr.net/npm/remixicon#2.5.0/fonts/remixicon.css");
In my case, it was failing because using external styles
import "https://unpkg.com/leaflet#1.6.0/dist/leaflet.css";
Fixed with using via npm:
npm i leaflet
import 'leaflet/dist/leaflet.css';
Official docs

Laravel 9 - using vite to do a js import within a blade template

I am trying out Laravel 9 with Vite, but without(!) a frontend framework (like Vue or React).
I am trying to make a reusable js module that I could use/load in different Blade templates.
So I created a file 'menu-btn-toggle.js' in 'resources/js/components/utilities'.
export class MenuBtnToggle
{
constructor() {
console.log('MenuBtnToggle');
}
}
I then created a 'utilities.js' file in 'resources/js/components'.
import './utilities/menu-btn-toggle';
I then updated the 'app.js' file in 'resources/js'
import './bootstrap';
import './components/utilities';
import Alpine from 'alpinejs';
window.Alpine = Alpine;
Alpine.start();
So far, this seems to be working (?). Atleast, i think so. Vite is not giving me any errors so far (yay!)
But then I try to import the module in a blade template, but I can't get it to work.
So at the bottom of my blade template, I added this:
<script type="module">
import MenuBtnToggle from 'menu-btn-toggle';
new MenuBtnToggle();
</script>
But this gives me the following error:
Uncaught TypeError: Error resolving module specifier “menu-btn-toggle”. Relative module specifiers must start with “./”, “../” or “/”.
But when I add one of the examples, the error changes to:
Loading module from “[local_dev_url]/menu-btn-toggle” was blocked because of a disallowed MIME type (“text/html”).
as it isn't be able to find the module. It returns a 404.
Is it even possible do to want I want without a frontend framework? Or am I not seeing something obvious? I have experience with php, unfortunately not (enough) with Laravel and Vite.
I was stuck into almost same situation where we are not able to use import statement inside our blade files using Vite. We commonly get
Uncaught TypeError: Error resolving module specifier
“menu-btn-toggle”. Relative module specifiers must start with “./”,
“../” or “/”.
or We get 404 Blocked error nearly same as
Loading module from “[local_dev_url]/menu-btn-toggle” was blocked
because of a disallowed MIME type (“text/html”).
When importing external npm packages or modules, we need to put them in windows object just like you did above for Alpine
window.Alpine = Alpine;
But for your specific case problem you must do following
1)Inside menu-btn-toggle.js
export class MenuBtnToggle
{
constructor() {
console.log('MenuBtnToggle');
}
}
2)Inside utilities.js
import {MenuBtnToggle} from "./utilities/menu-btn-toggle";
window.MenuBtnToggle = MenuBtnToggle;
And then finally inside your app.js
import './components/utilities';
Now inside your .blade file, there is no need to import the MenuBtnToggle
instead just do it like
new MenuBtnToggle();
No need to use import MenuBtnToggle from 'menu-btn-toggle'; in blade.
Important thing is to import your app.js file properly inside .blade file through vite
#vite(['resources/css/app.css','resources/js/app.js'])
And it works. It has worked when reproducing same files/setup as yours.

Cannot Import OrbitControls using Node 12 ES6 Import

I'm Using Node 12 (experimental-modules) and three (npm) and i cant get Imports to work for OrbitControls.js. I have index.js as "script: module".
Neither of these ES6 imports work
I tried copying the OrbitControls.js file out of the js folder (from the root folder of three) and placing it adjacent to index.js then adding
import {OrbitControls} from "./OrbitControls.js"
It did not work i receive the error
Uncaught SyntaxError: The requested module './OrbitControls.js' does
not provide an export named 'OrbitControls'
So I also tried to use the three library
import {OrbitControls} from "/three/examples/jsm/controls/OrbitControls";
returns 404 error, so i tried relative imports
import {OrbitControls} from "../../node_modules/three/examples/jsm/controls/OrbitControls.js";
got the 404 error again.
Ive also tried (something another user reccommended) const OrbitControls = new THREE.OrbitControls but the error seems happen from the ES6 import alone.
I haven't used experimental-modules, but your second example should read
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
without the / before three;
If that doesn't work, you could try copy-pasting the OrbitControls.js source code from here to your own folder.
import { OrbitControls } from "./myFolder/OrbitControls";
If this works, then it might be a problem with your node_modules installation.

importing js file in Vue project throws error

I am trying to use this plugin https://www.npmjs.com/package/mobius1-selectr
I did a npm install mobius1-selectr
and imported it in my main.js
import 'mobius1-selectr/dist/selectr.min.css';
import 'mobius1-selectr/dist/selectr.min.js';
but as soon I import I get this exception:
You must supply either a HTMLSelectElement or a CSS3 selector string.
which comes from the source of selectr.
Is the import not to be done this way in webpack / main.js?
What am I doing wrong? Any help is appreciated.
Yes your imports are incorrect. This library uses UMD to export Selectr constructor. You are using Webpack so default import in your case should work:
import Selectr from 'mobius1-selectr'

Categories

Resources