我正在尝试导入一个我用npm下载的模块。
我的json文件是:
{
"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",
},
}
我有我的html文件:
<!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';
}
和server.js文件:
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);
但是当运行代码时,我有错误
Uncaught TypeError: Failed to resolve module specifier "node-pandas"相对引用必须以"/"、"/"或"…/"开头。
我试过了,但它也给出了另一个错误。
如果我写:
import pd from './node-pandas';
或
import pd from '../node-pandas';
我:
GET http://localhost:1337/node-pandas net::ERR_ABORTED 404 (Not Found)
我的项目有这样的结构:
- 。
- server.js
- functions.js
- index . html
- package.json
- node_modules
- node-pandas
我使用的是visual studio 2019
你知道我做错了什么吗?
我也有同样的问题(我是一个完全的初学者)。
我通过NPM安装了一个依赖项,在这个项目中,他们使用了import语句,如
import pd from './node-pandas';
不是
import pd from './node-pandas.js';
所以我有一个情况,Live Server托管javascript文件在"http://localhost/scriptname.js"当项目在"http://localhost/scriptname"上寻找该文件时;(没有.js扩展名)
我当然可以在import语句中添加.js,但是我必须为这个外部项目的所有文件上的所有import语句这样做。
很奇怪,没有很多关于这个错误的信息,但总的来说:
- 存在一些模块说明符和相对导入引用:https://sbcode.net/threejs/module-specifiers/
- 只能使用相对导入引用。如果你想使用模块说明符,你需要一个工具来解析那些 WebPack就是这样一个例子。它会帮你解决所有这些import语句并生成一个javascript文件你可以把它添加到你的html文件中而不是
import pd from './node-pandas';
还要检查您的node-pandas中是否有任何默认导出。如果没有默认导出,则必须使用
import { moduleName } from './node-pandas';
这里的模块名称将与导出时的名称完全相同,并且用于多个您应该使用逗号
将它们分开。希望这能解决你的问题
const pd = require("node-pandas")
使用require代替import关键字可能会起作用,因为import是特定于es6和es7的
,另一方面,require在Node项目中由常规js支持
您需要提供模块的相对路径,如'assets/node-pandas/dist/node-pandas'
import pd from 'node-pandas';
##这里node-pandas模块位于根目录##
我建议使用。/或…/在模块名之前,提供的信息不足以提供解决方案