简体   繁体   English

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

[英]Uncaught Error: Cannot find module 'openpgp'

I'm learning javascript, and I tried to import some modules using npm in my js file. 我正在学习javascript,并且尝试在js文件中使用npm导入一些模块。 To import some module using require() it works fine, but I don't know why, for openpgp.js I got a 要使用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');导入模块var openpgp = require('openpgp'); ,

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

For information, when I using the node console, modules works fine! 有关信息,当我使用节点控制台时,模块工作正常!

I'm really new in javascript, so may be I do something wrong. 我真的是javascript新手,所以可能是我做错了。 If someone have an idea it's would be helpful ! 如果有人有想法,这将是有帮助的!

In order to require Node modules in browser script as in Node you need an external library. 为了像在Node中一样在浏览器脚本中要求Node模块,您需要一个外部库。 For example, you can use browserify lib. 例如,您可以使用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. Browserfiy网站和文档应该足够清晰。 I report here main steps: 我在这里报告主要步骤:

  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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM