首页 > 解决方案 > 未捕获的类型错误:无法解析模块说明符(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!' })

标签: javascripthtmlnode.jsexpressactor

解决方案


作者在这里。我不太清楚这里的确切问题是什么,但就目前而言,Nact 仅在服务器端得到官方支持。你 也许可以让它在浏览器中工作,但你可能需要 polyfillsetImmediate才能这样做。

由于 Nact 是一个 commonjs 模块,因此您需要一个像 Parcel 或 Webpack 这样的捆绑器才能将库转换为适当的模块格式。


推荐阅读