1

when import a package this error appear:

Uncaught TypeError: Failed to resolve module specifier "axios". Relative references must start with either "/", "./", or "../".

and when add " ./ " , " ../ " this error appear

GET http://localhost:8080/js/axios net:: ERR_ABORTED 404 (Not Found)

package.json

{
  "type": "module",
  "name": "fives",
  "version": "1.0.0",
  "description": "game",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./app.js"
  },
  "author": "ayah alrifai",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.0.15",
    "rimraf": "^3.0.2"
  },
  "dependencies": {
    "axios": "^0.19.2",
    "express": "^4.17.1",
    "socket.io": "^2.3.0",
    "socket.io-client": "^2.3.0"
  }
}

app.js

import http from "http";
import express from "express";
import io from "socket.io-client";
import path from 'path';

const app=express();
const port="8080";

app.use('/image', express.static('./public/img'));
app.use('/css', express.static('./public/css'));
app.use('/js', express.static('./js'));

app.get('/wait', (req, res)=> {
    res.sendFile(path.join(path.resolve()+'/html/await.html'));
});

app.get('/play', (req, res)=> {
    res.sendFile(path.join(path.resolve()+'/html/play.html'));
});

app.listen(port);

wait.js

import {getWaitedPlayers,createUser,deleteUser,isSelected,action} from "./fivesApi.js";
//code

fivesApi.js

import axios from "axios";
//code

tree

.
├── html
│   ├── wait.html
│   └── play.html
├── app.js
├── js
│   ├── wait.js
│   ├── fivesApi.js
│   └── play.js
├── node_modules
├── package.json
├── package-lock.json
└── public
    ├── css
    │   └── fives.css
    └── img
        ├── 2.png
        ├── 5.png
        └── ayah.png

2 Answers 2

1

This worked for me in a (somewhat) similar situation. Posting in case it helps someone. I do feel there's a better way, but it escapes me for now.

context: using an express.js server to serve up an entire client site that lives in a subfolder, and linking up a socket.io client via esm in the client JS module.

  1. In the express server, define a path to your node_modules directory:

    const DIR_ROOT = dirname(fileURLToPath(import.meta.url)); app.use('/node_modules', express.static(join(DIR_ROOT, "node_modules")));

  2. In the client module, provide the explicit pathname exactly as it appears in your file system:

    import { io } from "/node_modules/socket.io/client-dist/socket.io.esm.min.js";

  3. And of course, make sure this second bit is in a js module, so the script import looks like this:

Sign up to request clarification or add additional context in comments.

1 Comment

Huh, not sure why the SO markup isn't formatting my code properly. Perhaps some nice mod can come and clean that up. I'm stumped.
0

Spent 2 hours and finally find a solution, the first thing you need to do is

npm i parcel-bundler -D

then package.json add the following two-line of codes

"scripts": {
"dev": "parcel index.html",
"build": "parcel build index.html"
}

finally npm run dev.

and if you still have trouble, please open this link it just saves me so many hours.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.