0

I'm not a js expert, and my question might be out of scope, but I've been struggling for a while and tried so many solutions I have found here and here and more, but no success!!

this is the app hierarchy:

app
 |
 public
 |
 static
 |
 node-modules
            |
            connection
server.js
package.json

this is package.json

{
 "name": "appp",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "type": "module",
 "scripts": {
 "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [],
 "author": "me",
 "license": "MIT",
 "dependencies": {
 "connection": "file:../dbconnection",
 "express": "^4.17.1",
 "pg": "^8.7.1"
 },
 "devDependencies": {
 "@types/node": "^16.6.1"
 }
}

I assume the problem comes from "connection": "file:../dbconnection", since I think it shouldn't have file: in front of it. I have installed that via npm install --save dbconnection.

dbconnection module index.js:

import pkg from 'pg';
const {Client} = pkg;

        export default client =  new Client({
            host:"localhost",
            port: 5432,
            user: 'sha13',
            password: "ShieldTec2021",
            database: "shieldtec"
        });

I also have tried to export default class from the module and export functions from that too, but didn't work.

the error I get every time is the same: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

in another .js file I try

import { client } from '../../../node_modules/connection /index.js';

what cause the problem?

Talkhak1313
  • 301
  • 2
  • 11
  • The problem is that your server is returning an HTML content (at least according to the `content-type` header) when the browser is requesting a JS file (probably with a `script` tag). Can you post your ExpressJS routes handlers? Are you using the static middleware? – Ernesto Stifano Aug 16 '21 at 15:12
  • Why are you doing `npm install --save dbconnection`? `dbconnection` is not an existing NPM package. Are you trying to import something from one file into another? – Ernesto Stifano Aug 16 '21 at 15:14
  • I'm using static middleware `app.use("/static", express.static(path.resolve("frontend", "static"))); app.get("/*", (req, res) => { res.sendFile(path.resolve("frontend", "index.html")); }); ` – Talkhak1313 Aug 16 '21 at 15:17
  • @Ernesto I have npm init in my module (dbconnection), so I was able to install that via npm – Talkhak1313 Aug 16 '21 at 15:19
  • I'm trying to make a connection to database in a js file and export that to index.js. I'm using Postgresql and to be able to connect to that I need to use `const {Client}=require('pg')`, but as my app is modular (ES) I can't export anything from that. so I changed that to `import pkg from 'pg'; const {Client} = pkg;` – Talkhak1313 Aug 16 '21 at 15:28

0 Answers0