npm 安装后找不到导入的库

imported library not found after npm install

以下步骤来自:https://github.com/appfeel/node-pushnotifications#installation

我已经安装了软件包:

npm install node-pushnotifications --save
+ node-pushnotifications@1.1.9
updated 1 package and audited 244 packages in 14.663s
found 0 vulnerabilities

使用import PushNotifications from 'node-pushnotifications';

returns 错误:

> import PushNotifications from 'node-pushnotifications';
import PushNotifications from 'node-pushnotifications';
       ^^^^^^^^^^^^^^^^^

我不熟悉这种导入库的方法。对于我使用过的所有其他人,例如快递,进口如下:

var express = require('express');

我是不是漏了一步?

如何安装和导入PushNotifications?

尝试:

  • package.json
  • 中删除 node-pushnotifications
  • 通过 npm i node-pushnotifications
  • 安装
  • 否则,您可以使用 install ny yarn

改变import PushNotifications from 'node-pushnotifications';

var PushNotifications = require('node-pushnotifications')

已解决问题。

Node.JS v10.4.1 不支持 ES6 模块。虽然将 import 语句更改为 require 会起作用,但如果您想保留 import 语法,还有几个其他选项也可以起作用。

  • 将 Node.js 升级到 Node.js v12 - 对于 this 版本,您可以通过在 package.json 中添加 "type: "module" 来启用 import 语法].您还必须在启动服务器时添加一个 --experimental-modules 标志。
  • 使用 Babel 在您的应用程序中启用 import 语法。