【问题标题】:imported library not found after npm installnpm install 后找不到导入的库
【发布时间】:2019-06-11 09:06:38
【问题描述】:

以下步骤来自: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';

返回错误:

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

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

var express = require('express');

我错过了一步吗?

如何安装和导入 PushNotifications ?

【问题讨论】:

  • 你在使用 babel 之类的东西来支持 ES6 模块吗?
  • 你使用的是什么版本的节点?
  • @UmairSarfraz node -v v10.4.1

标签: node.js npm npm-install node-modules


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    更改import PushNotifications from 'node-pushnotifications';

    var PushNotifications = require('node-pushnotifications')

    解决了问题。

    【讨论】:

      【解决方案3】:

      试试:

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

      【讨论】:

        猜你喜欢
        • 2015-02-26
        • 2016-07-27
        • 2020-03-25
        • 1970-01-01
        • 2016-02-08
        • 2019-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多