Failed to resolve module specifier "three" as of 137 - javascript

Coming back to some old project, I discover nothing was loading. Checking the console log I see the following:
Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".
In my script I got the following:
<script type="module">
import * as THREE from "https://cdn.skypack.dev/three/build/three.module.js"
import { OBJLoader } from "https://cdn.skypack.dev/three/examples/jsm/loaders/OBJLoader.js"
import { FirstPersonControls } from "https://cdn.skypack.dev/three/examples/jsm/controls/FirstPersonControls.js"
I try to step back some versions and not until I reach 0.136.0 does this error go away. Has the way one imports three.js changed, or is this a bug?
Also, would there be some way of catching this at runtime? Like I get the latest version number from Skypack and try an import. If failed, automatically step back a decimal and try again.
Edit
#Mugen87 offers using importmap. I change my code with the following:
<script type="importmap">
{
"imports": {
"three": "https://cdn.skypack.dev/three/build/three.module.js",
"three/OBJLoader": "https://cdn.skypack.dev/three/examples/jsm/loaders/OBJLoader.js",
"three/FirstPersonControls": "https://cdn.skypack.dev/three/examples/jsm/controls/FirstPersonControls.js"
}
}
</script>
<script type="module">
import * as THREE from "three"
import { OBJLoader } from "three/OBJLoader"
import { FirstPersonControls } from "three/FirstPersonControls"
I get the following error:
Uncaught SyntaxError: The requested module '/-/three#v0.137.5-HJEdoVYPhjkiJWkt6XIa/dist=es2019,mode=raw/build/three.module.js' does not provide an export named 'default'

The CDN you're using (cdn.skypack.dev) does some re-exporting on their side, which is causing the problem.
The file you're referencing in your import isn't actually the three.js module, but a re-direct.
That file does an export * (which is fine), AND an export {default}, which is where the failure is occurring. Three.js does not have a default export.
Skypack is likely applying this redirect universally to all modules, so it's not that they necessarily KNOW this is happening, but they should definitely be testing their system before hosting a specific module...
Try changing your importmap to reference the following URL, and it should work:
https://cdn.skypack.dev/-/three#v0.137.5-HJEdoVYPhjkiJWkt6XIa/dist=es2019,mode=raw/build/three.module.js

Related

Module not found: Error: Can't resolve - Parsed request is a module

I have class components based project (a requirement by company, can't change to function components)
Once I ran elint --fix and started the project again, it gives me this error just for one file.
Parsed request is a module
using description file: C:\Users\Computer\Desktop\shop\package.json (relative paist or is not a directory
C:\Users\Computer\Desktop\shop\src\node_modules doesn't exist or is not a directory
looking for modules in C:\Users\Computer\Desktop\shop\node_modules
The file only contains an exported function that I use elsewhere to dispatch data. I'm not sure why it's looking in node_modules or how to fix this error.
The file looks something like this :
import store, { addToCart } from '../redux/store'
export function addToCartFunc (---props---) {
---code ----
store.dispatch()
}
I have no idea why this was happening, my guess it was recognizing the file as some kind of webpack but I fixed it by importing it as :
import { addToCartFunc } from './addToCartFunc.js'
instead of (like all others):
import { addToCartFunc } from './addToCartFunc'

Call #metaplex/js from html

i want to know how i can import the #metaplex/js module into a js file connected to the html. If i use the import statement i get an error saying "Import identifiers must start with ".", "./"" and things like this. For solanaWeb3 i had to add a script tag with a unpkg link as src and its working. Id like to know where can i find the same script src for #metaplex.js so i can use that too ;).
import { programs } from "#metaplex/js"
this is the import statement used and this is the error i get
Uncaught TypeError: Error during the resolution of the module identificator “#metaplex/js”. Module identificators must start with “./”, “../” o “/”.
You could try:
<script src="https://unpkg.com/#metaplex/js#latest/lib/index.iife.min.js">
</script>

Import mathjs as a JavaScript module fails

I try to import mathjs as a module in a browser (client side). I copied this file on the server, and then try to import the JS module, but it always fails. Here are some trial I already made:
import { mathjs } from "./math.min.js"
SyntaxError: The requested module './../modules/math.min.js' does not
provide an export named 'mathjs'
import { math } from "./math.min.js"
SyntaxError: The requested module './../modules/math.min.js' does not
provide an export named 'math'
import { create, all } from "./math.min.js"
SyntaxError: The requested module './math.min.js' does not provide an export named 'all'
I also try:
import("/math.min.js");
Uncaught (in promise) TypeError: Cannot set property 'math' of undefined
Note that the path is OK, I checked. I'm probably missing something, but I can't find what I am doing wrong. Any help is welcome.
Here is a repl.it with the code: https://repl.it/#FifiLulu/NewJollyRoutes
You'll need to add:
<script type="module">
to your html file.

catch error `module must start with either "/", "./", or "../"`

Example
<!-- index.html -->
<script type="module">
import 'someModule'
</script>
TypeError: Error resolving module specifier: someModule
Uncaught TypeError: Failed to resolve module specifier "someModule". Relative references must start with either "/", "./", or "../".
Q:
Is it possible to catch this error?
Who dispatch this error?
Context
In this case, the dependency can be caught and resolved using the service worker.
<script type="module">
import '/someModule'
</script>
You are not importing it in right way as per the error log.
Valid module specifiers must match one of the following:
A full non-relative URL. As in, it doesn't throw an error when put through new URL(moduleSpecifier).
Starts with /.
Starts with ./.
Starts with ../.
Other specifiers are reserved for future-use, such as importing built-in modules.
Just try this code:
<script type="module">
import {someModule} from './someModule.js';
</script>
You cannot catch that error, unless you use some non-standard uncaught exception handler, like window.onerror in browser or process.setUncaughtExceptionHandler in Node.js.
Catching that error is impossible because import statements are allowed only outside of any blocks, in the module-global scope. Therefore, if you try to wrap the import into a try..catch, it will throw a SyntaxError.
Instead, you could use the dynamic import() syntax proposal, which can be called anywhere; and handle its promise rejection.

SystemJS, using importmap with module, that needs react

I have created a javascript library with webpack, that outputs a systemjs module. This module has a dependency on react, which I specified as an external.
The resulting javascript file starts like this:
System.register(["react"], function(__WEBPACK_DYNAMIC_EXPORT__) {
var __WEBPACK_EXTERNAL_MODULE_react__;
return { ....
Additionally I have an app, that uses SystemJS during runtime to load that module. In order to provide the react dependency, I have defined an importmap:
{
"imports": {
"react": "https://unpkg.com/react#16.11.0/umd/react.production.min.js"
}
}
And the part, where I import the module, looks like this:
const modulePromise = System.import(MODULE_URL);
modulePromise.then(module => {
console.log('module loaded successfully!');
});
The problem now is, that the console.log is never called, because I get a TypeError, that says, that "Component is not a property of undefined", which tells me, that somehow react has not correctly been passed to my module.
To be precise, in the browser network tab I see, that my module and the react import is indeed loaded, but somehow it is not correctly processed.
Has anyone an idea, what i might be doing wrong?
OK, so eventually I solved this myself, although a bit different.
First, I did not use the unpkg link anymore, but I actually include React as a library in my main app.
And I changed my importmap to:
<script type="systemjs-importmap">
{
"imports": {
"react": "app:react",
"react-dom": "app:react-dom"
}
}
</script>
Also in the main app I use System.set(...) from SystemJs to tell SystemJS where to find the 'app:react' and 'app:react-dom' dependencies:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import 'systemjs/dist/system.min';
...
System.set('app:react', { default: React, __useDefault: true });
System.set('app:react-dom', { default: ReactDOM, __useDefault: true });
And now, if I load my module with that external react dependency through SystemJS, it works.

Categories

Resources