【问题标题】:Why is my NPM package is not working in Laravel?为什么我的 NPM 包在 Laravel 中不起作用?
【发布时间】:2020-11-08 21:15:14
【问题描述】:

我为自己创建了一个 npm 包,但是当我尝试在 laravel 中使用它时,它返回了一个 ReferenceError。 所以这就是我所做的: 我遵循了关于如何启动 npm 包的教程,它在标准 javascript 中应该如何工作。 所以我在我的 laravel 应用中做了以下操作:

npm install @ilayalmalem/lingojs

成功了,并且在 node_modules 文件夹中实例化了一个文件夹。

我在 app.js 中添加了以下内容:

window.lingo = require('@ilayalmalem/lingojs');

但是当我尝试在这样的刀片视图中使用它时:

var lingo = new Lingo('.s',{
     Models:{
       model1:'d'
     }
});

返回:

Uncaught ReferenceError: Lingo is not defined

这是我的 package.json:

{
  "name": "@ilayalmalem/lingojs",
  "version": "0.0.4",
  "description": "Write less javascript.",
  "main": "index.js",
  "scripts": {
    "test": "lingo"
  },
  "dependencies": {
    "jquery": "^3.2.1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ilayalmalem/Lingo.js.git"
  },
  "keywords": [
    "LingoJS",
    "Backend",
    "communication"
  ],
  "author": "Ilay David Almalem",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ilayalmalem/Lingo.js/issues"
  },
  "homepage": "https://github.com/ilayalmalem/Lingo.js#readme"
}

这是我实例化 Lingo 类的方法:

class Lingo {
...code
}

如果您真的对代码感兴趣,请检查以下内容: https://github.com/ilayalmalem/Lingo.js

如果您需要更多信息,请发表评论。

【问题讨论】:

  • new window.lingo()怎么样
  • @dbf 谢谢你的回答!但我只是回答了。谢谢!

标签: javascript node.js laravel npm package


【解决方案1】:

伙计们,如果你遇到这个问题,只需将这一行添加到你的 index.js 中:

module.exports = your class;

别忘了运行:

node your_main_file.js

在命令行中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2020-11-01
    • 2015-01-29
    • 1970-01-01
    相关资源
    最近更新 更多