解决 SyntaxError: Cannot use import statement outside a module 报错
发布于 4 年前 作者 luojiyin1987 40367 次浏览 来自 分享

示范代码 (最近在学 RxJS) import { range } from ‘rxjs’; import { map, filter } from ‘rxjs/operators’; range(1, 200).pipe( filter(x => x % 2 === 1), map(x => x + x) ).subscribe(x => console.log(x));

报错 import { range } from ‘rxjs’; ^^^^^^

SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:1071:16) at Module._compile (internal/modules/cjs/loader.js:1121:27) at Object.Module._extensions…js (internal/modules/cjs/loader.js:1177:10) at Module.load (internal/modules/cjs/loader.js:1001:32) at Function.Module._load (internal/modules/cjs/loader.js:900:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47

环境信息 MacOS 10.11.6 Node 12.16.0
猜测 node 对ES6 Imports 支持 有限。 google 关键词 node es6 import run so 链接 https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node 高赞答案 要上Node13 , 尝试 第二个答案, 安装esm 解决问题

回到顶部