Failed to resolve module specifier when trying to import any module - javascript

Everytime I try to import a module I get this error Failed to resolve module specifier "mongodb". Relative references must start with either "/", "./", or "../". and I cant seem to find the answer anywhere. Im just trying to import mongodb import { mongoClient } from "mongodb";. Is there any solution to this?
I tried doing what the error says or tried using require keyword but nothing works. I cant use any module

Not sure if this will help, I was having the same exact problem and I was just trying to run this all from JS files getting loaded into a static html page. Needed to setup an Express.JS server run with NodeJS and managed by PM2 that creates the connection and makes all the calls when certain endpoints are reached.

Related

Amplify React build fail with "Module not found: Error: Can't resolve './components/...' in '/codebuild/output/src313005886/src/client-portal/src' "

for some time, I have been wrapping my head around an error preventing me from deploying my react app to AWS Amplify. The compiler seems not to be able to resolve a component during the build phase. I keep getting the following error Module not found: Error: Can't resolve './components/...' in '/codebuild/output/src313005886/src/client-portal/src; please see the attached error output bellow.
Here is the file structure:
Here is how the file is imported:
import AdminCreateEvaluation from "./components/evaluationgeneratorform";
I have tried several solutions, including renaming my files, but the build won't just compile;
Any help will be appreciated.
Thank you.
Check if you have changed the "Components" from uppercase to lowercase "components". In my case, I changed it after it had been created on GitHub. But GitHub doesn't update this change. When it's cloned to Amplify host, it's still "Components", so the build failed.
I had to change the name to something else and then change it back to fix the issue.

Javascript, Node.js and browser: Import from folder does not work in web app?

This import of a folder (#terra-money/terra.js/)
import {LCDClient, Coin} from "#terra-money/terra.js"
works without problems when running the file with node.js
But when I want to use the same expression in a web app I get the error message
"Uncaught TypeError: Failed to resolve module specifier "#terra-money/terra.js". Relative references must start with either "/", "./", or "../"."
... so the problem seems to be that the import does not point to a single .js-file, right? Is there a way to make this work?

import module without nodejs - vanillaJS

I'm refreshing my old and low knowledge on VanillaJS using the latest best practices i found.
I recently did a tutorial on NodeJS doing API REST with ExpressJs and one with Socket IO.
Now I want to practice a little before going to REACTJS.
So I started a little project
I do one without NodeJs - just JS into HTML view - using Objects.
I want to use modules from Npm. I want to try Fakerjs but when i want to import it i have a 403.
The path is correct, no error.
So i'm wondering if it's possible without Nodejs to import modules when doing VanillaJs?
Am i doing it wrong ?
The structure of the project is :
js/
main.js
class/
node_modules/
index.html
Main.js:
'use strict'
//Importation of modules
import faker from '../node_modules/faker'
//Importation of Class Folder
import { Crypto } from "./class/crypto.class.js";
console.log(faker);
faker.locale = 'en_US';
I have this error in console:
GET http://crypto-market-js.local:8282/node_modules/faker/ net::ERR_ABORTED 403 (Forbidden)
If i write : import faker from 'faker' (like with node js but it's a require instead) i have this : Uncaught TypeError: Failed to resolve module specifier "faker". Relative references must start with either "/", "./", or "../".
If you could give me a hand on that plz :)
Trying to import with require is not supported without NodeJS, because require is a node-specific function. When trying to import modules into Vanilla JS, my recommendation is to first link to ths script with the <script> html tag, and then add another script with import faker from 'faker'
Hope that clarifies your issue.

Having trouble importing map from jquery

I am using a function to split words into syllables in Javascript within my ASP .NET MVC project, and am trying to implement the map function within my js file. When I type out the function, it recognizes it as a real function, and does an auto import from jquery that looks like this:
import { map } from "jquery";
When I run that, I get the error "Uncaught SyntaxError: Cannot use import statement outside a module". So I spent a while researching that, and read a post where it said you had to add type="module" to your script import within the view. So I ended up with this:
<script src="~/Scripts/sentence.js" type="module"></script>
After I tried to run it with the type identified, I get the error: "Uncaught TypeError: Failed to resolve module specifier "jquery". Relative references must start with either "/", "./", or "../"."
When I researched that for a while, I found an article that said you had to import them like this:
import $ from './libs/jquery/dist/jquery.js' or
import $ from '/js/libs/jquery/dist/jquery.js'
and I get the error: "GET https://localhost:44352/js/libs/jquery/dist/jquery.js net::ERR_ABORTED 404"
Now I am kind of lost. I feel like it should not be this difficult to use a simple function like map(), but here I am. Can anyone give any advice or a path that I should follow to figure this out? Thanks!
The articles/documentation that tells you to import from a relative or absolute url are assuming you actually have your own copy of the library at that relative or absolute url. You can then solve your problem by including your own copy of jquery in your project. If you don't want to deal with managing the jquery dependency locally, you can always import the library from a cdn link:
import { map } from "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js";

Require in react can't resolve path

I'm trying to use tracking-js library in my project I'm using react but I dont know if I'm doing anything wrong but keep showing that the module is not found, I already check my package.json and the module install. So this is how I require the module:
const tracking = require("tracking");
what am I doing wrong?
For node, including node-based build tools, first make sure that the module is present. Keep in mind that require does not care about the package.json of your app, only about the module files being present.
Check if node_modules/tracking is present.
Make sure the "main" JS file can be found. If there's a node_modules/tracking/package.json file, see if it has a main property and if the file it references exists. If there is no main property, make sure there's an index.js in the root of the module directory.
If all this is fine and you're getting the module not found error at runtime in client-side JavaScript, then your bundling config may be incorrect, and your webpack/browserify/whatever config will have to be scrutinized for bugs.

Categories

Resources