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
Uncaught Error: Cannot find module 'openpgp'.
Here my 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
I run npm install openpgp
inside my js file.
In my js I import modules by typing var openpgp = require('openpgp');
,
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 !
In order to require Node modules in browser script as in Node you need an external library. For example, you can use browserify lib.
Browserify let you bundle your scripts with needed dependencies into a single file that can be included in your web page.
Browserfiy website and docs should be enough clear. I report here main steps:
Install browserify on your system:
npm install -g browserify
Bundle scripts with the require("openpgp")
with the following command:
browserify yourScriptName.js -o bundleFile.js
(where -o
parameter is for choosing the output file, if omitted browserify will print output on stdout)
Now you can include the previous output in your with web page with the following tag:
<script src="bundleFile.js"></script>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments