【发布时间】:2020-11-26 22:13:45
【问题描述】:
我正在使用带有 express 的节点服务器来使用 Portis SDK。项目结构如下:
├── app.js (entry)
├── node_modules
├── package.json
├── package-lock.json
├── public (static files)
│ └── portis.js
└── views (templates and rendering)
├── home.html
└── layouts
package.json
{
"name": "src",
"version": "1.0.0",
"description": "YYYYY",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "XXXXX",
"license": "ISC",
"dependencies": {
"@portis/web3": "^2.0.0-beta.59",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-handlebars": "^5.2.0",
"pm2": "^4.5.0",
"web3": "^1.3.0"
},
"devDependencies": {
"@babel/core": "7.2.0",
"@types/web3": "1.0.18",
"parcel-bundler": "^1.6.1",
"nodemon": "^2.0.6"
}
}
在我的一个模板 (home.html) 中,
<script src="/public/portis.js"></script>
而portis.js 与this CodeSandBox 相同。
提供该模板时,出现错误:
Uncaught SyntaxError: Cannot use import statement outside a module (portis.js:1)
从搜索 SO 结果来解决它,当我更改 home.html 并将模块类型添加到脚本时:
<script type="module" src="/public/portis.js"></script>
(虽然type="module" 没有在我复制的codesanbox 中使用,但它可以完美运行)
但它会导致另一个错误:
Uncaught TypeError: Failed to resolve module specifier "@portis/web3". Relative references must start with either "/", "./", or "../".
(如果我删除import "@portis/web3",那么它会在下一个导入行给出相同的错误)
我已经尝试了我在 SO 上找到的大多数相关解决方案,但它似乎不起作用
【问题讨论】:
-
尝试将 ... type="text/javascript" 放入您的脚本标签
标签: javascript node.js import