Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript import package Failed to resolve module specifier

Tags:

javascript

I am trying to import a module I have downloaded with npm.

My json file is:

{
  "name": "nodejs-web-app1",
  "version": "0.0.0",
  "description": "NodejsWebApp1",
  "main": "server.js",
  "author": {
    "name": ""
  },
  "dependencies": {
    "fs": "^0.0.1-security",
    "http": "^0.0.1-security",
    "node-pandas": "^1.0.5",
    "node-static": "^0.7.11",
    "require": "^2.4.20",
  },
}

I have my html file :

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Home</title>
    <script type="module" src="functions.js"></script>
    <link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
    <h1>Dashboard</h1>
    <p>Welcome!</p>
    <button onclick="scraper()">CLICK</button>
    <label id="label">Reponse</label>
</body>

</html>

my functions.js file

import pd from 'node-pandas';
function scraper() {
    const s = pd.Series([1, 9, 2, 6, 7, -8, 4, -3, 0, 5])
    console.log(s);
    document.getElementById('label').innerHTML = 'A computer science portal for geeks';
}

and my server.js file:

var nStatic = require('node-static');
var http = require('http');
var fs = require('fs');
var port = process.env.PORT || 8080;

var file = new nStatic.Server(__dirname);

http.createServer(function (req, res) {
    file.serve(req, res);
}).listen(port);

But when running the code I have the error

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

I tried but it gives another error also.

If I write:

import pd from './node-pandas';

or

import pd from '../node-pandas';

I get:

GET http://localhost:1337/node-pandas net::ERR_ABORTED 404 (Not Found)

My project has this structure:

  • myproject
    • server.js
    • functions.js
    • index.html
    • package.json
    • node_modules
      • node-pandas

And I am using visual studio 2019

Any idea what I am doing wrong please?

like image 454
Nicolas Rey Avatar asked Aug 18 '26 06:08

Nicolas Rey


1 Answers

I had the exact same issue (I'm a total beginner).

I installed via NPM a dependency and within that project they were using import statement like

import pd from './node-pandas';

instead of

import pd from './node-pandas.js';

So I had a situation where Live Server was hosting the javascript file on "http://localhost/scriptname.js" while the project was looking for that file instead on "http://localhost/scriptname" (without the .js extension)

I could off course add .js to the import statement, but then I would have to do that for ALL the import statements on all the files of this external project.

It's strange that there is not a lot of information about this error, but in summary:

  • There exists something as Module Specifiers and Relative Import References: https://sbcode.net/threejs/module-specifiers/
  • You can only use relative import references. If you want to use Module Specifiers, you need a tool to resolve those
  • An example of such tool is WebPack. It will resolve all these import statement for you and generate one javascript file instead that you can add to your html file instead
like image 164
Matt Avatar answered Aug 20 '26 00:08

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!