不能做到这一点我觉得很傻,但我正在尝试创建一个简单的项目,在VS Code中的Node.js上使用一些RXJS。
mkdir test
cd test
code .
type nul > app.js
npm init
npm install rxjs
app.js包含
import * as rxjs from 'rxjs';
rxjs.of(1, 2, 3);
var msg = 'Hello World';
console.log(msg);
调试应用程序时出现运行时错误:(node:9988) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
所以我补充道:
"type": "module"
到package.json:
{
"name": "test",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "app.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"rxjs": "^6.5.4"
}
}
现在我得到了TypeError: rxjs.of is not a function
为什么做这么简单的事情这么复杂?这是怎么回事?
而不是
import * as rxjs from 'rxjs';
使用
const rxjs = require('rxjs');
当前节点不支持esmodules