未捕获错误:找不到模块'openpgp'

时间:2021-10-26 20:43:52

I'm learning javascript, and I tried to import some modules using npm in my js file. To import some module using require() it works fine, but I don't know why, for openpgp.js I got a

我正在学习javascript,我试图在我的js文件中使用npm导入一些模块。要使用require()导入一些模块,它工作正常,但我不知道为什么,对于openpgp.js,我得到了一个

Uncaught Error: Cannot find module 'openpgp'.

未捕获错误:找不到模块'openpgp'。

Here my package.json :

这是我的package.json:

{
  "name": "User",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "openpgp": "^2.5.1",
    "truffle-artifactor": "^2.1.2"
 },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

Here the setup of my app : app config

这里是我的应用程序的设置:app config

  1. I run npm install openpgp inside my js file.

    我在我的js文件中运行npm install openpgp。

  2. In my js I import modules by typing var openpgp = require('openpgp');,

    在我的js中,我通过输入var openpgp = require('openpgp');来导入模块,

  3. Then I refreshed my browser and see my error : browser error
  4. 然后我刷新浏览器并看到我的错误:浏览器错误

For information, when I using the node console, modules works fine!

有关信息,当我使用节点控制台时,模块工作正常!

I'm really new in javascript, so may be I do something wrong. If someone have an idea it's would be helpful !

我是javascript的新手,所以我可能做错了。如果有人有想法,那会有所帮助!

1 个解决方案

#1


0  

In order to require Node modules in browser script as in Node you need an external library. For example, you can use browserify lib.

要像在Node中那样在浏览器脚本中要求Node模块,您需要一个外部库。例如,您可以使用browserify lib。

Browserify let you bundle your scripts with needed dependencies into a single file that can be included in your web page.

Browserify允许您将具有所需依赖关系的脚本捆绑到可包含在网页中的单个文件中。


Browserfiy website and docs should be enough clear. I report here main steps:

Browserfiy网站和文档应该足够明确。我在这里报告主要步骤:

  1. Install browserify on your system:

    在您的系统上安装browserify:

    npm install -g browserify
    
  2. Bundle scripts with the require("openpgp") with the following command:

    使用require(“openpgp”)使用以下命令捆绑脚本:

    browserify yourScriptName.js -o bundleFile.js
    

    (where -o parameter is for choosing the output file, if omitted browserify will print output on stdout)

    (其中-o参数用于选择输出文件,如果省略,则browserify将在stdout上打印输出)

  3. Now you can include the previous output in your with web page with the following tag:

    现在,您可以将以前的输出包含在带有以下标记的网页中:

    <script src="bundleFile.js"></script>
    

#1


0  

In order to require Node modules in browser script as in Node you need an external library. For example, you can use browserify lib.

要像在Node中那样在浏览器脚本中要求Node模块,您需要一个外部库。例如,您可以使用browserify lib。

Browserify let you bundle your scripts with needed dependencies into a single file that can be included in your web page.

Browserify允许您将具有所需依赖关系的脚本捆绑到可包含在网页中的单个文件中。


Browserfiy website and docs should be enough clear. I report here main steps:

Browserfiy网站和文档应该足够明确。我在这里报告主要步骤:

  1. Install browserify on your system:

    在您的系统上安装browserify:

    npm install -g browserify
    
  2. Bundle scripts with the require("openpgp") with the following command:

    使用require(“openpgp”)使用以下命令捆绑脚本:

    browserify yourScriptName.js -o bundleFile.js
    

    (where -o parameter is for choosing the output file, if omitted browserify will print output on stdout)

    (其中-o参数用于选择输出文件,如果省略,则browserify将在stdout上打印输出)

  3. Now you can include the previous output in your with web page with the following tag:

    现在,您可以将以前的输出包含在带有以下标记的网页中:

    <script src="bundleFile.js"></script>