javascript - 未捕获的类型错误:无法解析模块说明符(Node.js、Express、nact)
问题描述
我正在构建一个简单的应用程序,它使用Nact库演示演员模型。
我现在处理的错误是:
未捕获的类型错误:无法解析模块说明符“nact”。相对引用必须以“/”、“./”或“../”开头
这是 package.json:
{
"name": "actor-model-with-nact-demo-020120",
"version": "1.0.0",
"description": "Adam's Actor Model with Nact Demo",
"main": "server.js",
"dependencies": {
"express": "^4.17.1",
"nact": "^7.2.2",
"nodemon": "^2.0.2"
},
"devDependencies": {},
"scripts": {
"dev": "nodemon ./server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Adam Dudley",
"license": "MIT"
}
这是 server.js:
const express = require('express')
const path = require('path')
const app = express()
const port = process.env.PORT || '8000'
app.use(express.static(__dirname + '/public'))
app.use('/static', express.static('./static/'))
app.get('/static', function(req, res) {
res.sendFile(path.join(__dirname + '/static/app.js'))
})
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/views/index.html')
})
app.listen(port, () => {
console.log(`Listening to requests on http://localhost:${port}`)
})
这是 index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Adam's Actor Model with Nact Demo</title>
</head>
<body>
<h1>Adam's Actor Model with Nact Demo</h1>
<script src="static/app.js" type="module"></script>
</body>
</html>
这是 app.js:
import { start, dispatch, stop, spawnStateless } from 'nact'
const system = start()
const greeter = spawnStateless(
system,
(msg, ctx) => console.log(`Hello ${msg.name}`),
'greeter'
)
dispatch(greeter, { name: 'World!' })
解决方案
作者在这里。我不太清楚这里的确切问题是什么,但就目前而言,Nact 仅在服务器端得到官方支持。你 也许可以让它在浏览器中工作,但你可能需要 polyfillsetImmediate
才能这样做。
由于 Nact 是一个 commonjs 模块,因此您需要一个像 Parcel 或 Webpack 这样的捆绑器才能将库转换为适当的模块格式。
推荐阅读
- r - 从 emmeans 中的成对对比中提取概率
- html - 有没有办法将元素锁定在其容器的左右边缘,使其伸展以填充该容器?
- scala - 为什么scala允许从recoverWith返回
- php - PHP - 以 15 为增量计数,但最大为 45
- sql - Postgres中多个数组的平均值
- php - Laravel 5.5 使用除 ENV 之外的其他电子邮件设置发送电子邮件
- java - LinkedList 中的节点(整数索引)
- php - WooCommerce - 在结帐时显示最终运输选择
- mocking - Android espresso 匕首测试。在构造函数注入中模拟
- reactjs - 反应?有什么替代方法可以处理数据