Reputation: 481
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:
And I am using visual studio 2019
Any idea what I am doing wrong please?
Upvotes: 6
Views: 24923
Reputation: 1131
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:
Upvotes: 3
Reputation: 13
const pd = require("node-pandas")
Using require instead of import keyword might work as import is es6 and es7 specific
on the other hand require is supported by regular js as of in Node projects
Upvotes: -1
Reputation: 13
import pd from './node-pandas';
Also Check that do you have any default exports in your node-pandas or not. If there is no default export in that case you have to use
import { moduleName } from './node-pandas';
Here module name would be exact same name by which it's exported and for multiple imports you should seperate them using comma
Hope this solve your query
Upvotes: 0
Reputation: 13
import pd from 'node-pandas';
## Here node-pandas module is in some kind of root directory ##
I advise using ./ or ../ before your module name, Provided information is not enough to provide an solution
Upvotes: -1
Reputation: 25
You need to provide the relative path to the module like 'assets/node-pandas/dist/node-pandas'
Upvotes: -2