admin管理员组

文章数量:1389772

I have read the axios info at npm and at axios and I think I am doing what I am supposed to be doing.

I am trying to use axios in my .public/index.js file.

My index.js is where I access the DOM for eventListners etc. and where I would like to send and receive data to and from my data base.

path ./public/js/index.js'

I have axios imported like this in my ./public/js/index.js' import axios from 'axios';

When I start my app with nodemon server.js I get: Uncaught TypeError: Failed to resolve module specifier "axios". Relative references must start with either "/", "./", or "../".

I do not get any stack trace error messages in my terminal.

Some other info: npm list axios gives me [email protected] npm list express gives me [email protected]

Part of my package.json

type: "module" 
"main": "app.js",

  "scripts": {
    "start": "nodemon server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

 "dependencies": {
    "axios": "^1.8.3",
    "dotenv": "^16.4.7",
    "express": "^4.21.2",
    "mongoose": "^8.10.0",
    "nodemon": "^3.1.9",
    "pug": "^3.0.3"
  },

I hopefully expected any of these to work but none of them did.

In my .public/js/index.js I have also tried:

  import axios from 'axios/index.js';
  import axios from '../../node_modules/axios/index';

The error: GET http://127.0.0.1:3000/node_modules/axios/index net::ERR_ABORTED 404 (Not Found)

In my .public/js/index.js I have tried: import axios from '../../node_modules/axios/index'; GET http://127.0.0.1:3000/node_modules/axios/index net::ERR_ABORTED 404 (Not Found)

I have tried using axios in my home html while trying to use axios in my index.js file. (In html)

<script import axios from 'axios'></script>

I get index.js:60 Uncaught ReferenceError: axios is not defined at HTMLSelectElement.<anonymous>

and when I use this:

<script src='../../node_modules/axios'></script>type here

I get Refused to execute script from 'http://127.0.0.1:3000/node_modules/axios' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. Other html things I have tried:

These 2 don't work either.

and
   <script import axios from '../../node_modules/axios'></script>

and
  <script import axios from '../../node_modules/axios/index.js'></script>     

My home html has at the end.

  script(type='module', src='../js/index.js', defer)

My app.js

import * as path from 'path';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import bodyParser from 'body-parser';
import viewRouter from './routes/viewRoutes.js';
import dbControlRoutes from './routes/dbControlRoutes.js';
import AppError from './utils/appError.js';

const app = express();

// 
const __dirname = dirname(fileURLToPath(import.meta.url));
console.log('app.js __dirname: ', __dirname);

app.set('view engine', 'pug');
// eslint-disable-next-line no-undef
app.set('views', path.join(__dirname, 'views'));

// Serving static files
app.use(express.static(path.join(__dirname, '/public')));

app.use(
  '/',
  (req, res, next) => {
    console.log('Request URL:', req.originalUrl);
    next();
  },
  (req, res, next) => {
    console.log('Request method:', req.method);
    next();
  };

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

app.use(viewRouter);
app.use(dbControlRoutes);

app.all('*', (req, res, next) => {
  next(new AppError(`In ./app.js, CAN'T FIND ${req.originalUrl} on this server! req = `, 404));
});

export default app;

本文标签: htmlaxios throws Uncaught TypeError Failed to resolve module specifier quotaxiosquotStack Overflow