为什么我的NPM软件包不能在Laravel中使用?

时间:2020-07-19 14:24:45

标签: javascript node.js laravel npm package

我为自己创建了一个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

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

1 个答案:

答案 0 :(得分:0)

所以,伙计们,如果您遇到此问题,只需将这一行添加到index.js:

module.exports = your class;

不要忘记运行:

node your_main_file.js

在命令行中。