0
votes

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
1

1 Answers

0
votes

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.