undefined variable that equals to a node module
undefined variable that equals to a node module
I try to transpire my file using browserify or webpack.
This is my code:
let rangy = require('rangy')
rangy.init()
const rang = () => rangy
After transpire my code and add in a html page, I try to get rangy in console, but when i type rangy
and press enter button, js says:
rangy
Uncaught ReferenceError: rangy is not defined
When i type rang()
and press enter, js says:
rang()
undefined
But line 2 runs correctly.
I need to use rangy
in script tag of my index.html
.
rangy
index.html
How can I do this?
This is my transpired file:
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
let rangy = require('rangy')
rangy.init()
const rang = () => rangy
},{"rangy":2}],2:[function(require,module,exports){............
cosnt rang = () => rangy
thanks @CertainPerformance. but this is a typing mistake in this question only. I modified question.
– hamidreza
Jun 30 at 6:14
It's hard to tell from your question if you are using webpack or not, but if you are then the problem you are having is that webpack wraps everything in a function so that all the webpack stuff doesn't interfere with other, external javascript libraries. Could you provide more details on your webpack configuration and which version of webpack you are using?
– Peter Haight
Jun 30 at 6:25
I use the lastest version of webpack. But result in browserfy and webpack are same.
– hamidreza
Jun 30 at 6:28
Could you share the transpiled code?
– jelhan
Jun 30 at 16:38
1 Answer
1
If you use webpack, and you are ok with randy
leaking into the global scope, then just remove let
:
randy
let
rangy = require('rangy');
rangy.init();
randy
variable will become available in the console.
randy
thanks. it means also I can user window.rangy
– hamidreza
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
cosnt rang = () => rangy
Spelling matters in programming.– CertainPerformance
Jun 30 at 6:13