2

I have a react-create-app project that links a proprietary library in my public index.html that lets me use a variable called "SingularPlayer".

<script src="https://www.something.com/libs/singularplayer.js"></script>

for example I'm supposed to be able to:

const previewObj = SingularPlayer(iframeId);

But I can't because I keep getting this error:

Line 67: 'SingularPlayer' is not defined no-undef

I've tried putting these in my package.json but these didn't work either:

  "rules": {
    "no-undef": 0
  },
  "globals": {
    "SingularPlayer": false
  },
  "env": {
    "SingularPlayer": true
  }

EDIT: my project won't let me run (npm start) until I fix the error unfortunately

user1189352
  • 3,628
  • 12
  • 50
  • 90
  • 1
    If it's in the global namespace, try `window.SingularPlayer(iframeId)`. Or use `import` instead of a script tag. – Håken Lid Jul 23 '19 at 17:27
  • i tried window.SingularPlayer in my old project where it works and it doesn't work for some reason.... i can't tell you why... once i remove the "window." and just put "SingularPlayer" it works. I'll see if i can import the script – user1189352 Jul 23 '19 at 17:29
  • Try `"globals":{"SingularPlayer":"readonly"},` in the eslint config. https://eslint.org/docs/rules/no-undef#rule-details – Håken Lid Jul 23 '19 at 17:30
  • try adding your code inside `window.onload = () => SingularPlayer(...)` it's probably because your script is running before SingularPlayer is loaded. – Kousha Jul 23 '19 at 17:32
  • @HåkenLid that didn't work unfortunately but ty – user1189352 Jul 23 '19 at 17:34
  • @Kousha it's definitely not that, my project won't even let me npm start until i fix the error – user1189352 Jul 23 '19 at 17:35
  • What framework are you using? What exactly does `npm start` run? – Kousha Jul 23 '19 at 17:41
  • @Kousha React using their recommended react-create-app to get started – user1189352 Jul 23 '19 at 17:42
  • If you are using `javascript` you should just be able to do `window.SinglarPlayer()` I just did that, and my app loaded, but then crashed because that does not exist. – Kousha Jul 23 '19 at 17:49
  • @Kousha hm you may be right. i will experiment thank you – user1189352 Jul 23 '19 at 17:50
  • If you are using TypeScript you have a few options. The easiest/cheat way is to `declare const window: any` at the top of the page, now you can do `window.SingularPlayer()`. Or create a `global.d.ts` file and then `interface Window { SingularPlayer: () => void }` or whatever the interface is – Kousha Jul 23 '19 at 17:50
  • @Kousha using JS and JSX unfortunately – user1189352 Jul 23 '19 at 17:52
  • And using `window.SingularPlayer()` doesn't work? What's the error you get? – Kousha Jul 23 '19 at 18:00
  • i'm thinking window.SingularPlayer() should work technically. it's the only way it makes sense. I'm just figuring out why it's not working properly right now before I can say that at least – user1189352 Jul 23 '19 at 18:06
  • @HåkenLid window.SingularPlayer did work. if you want to submit an Answer i'll give you credit. thank you. – user1189352 Jul 24 '19 at 18:23

3 Answers3

0

You can bypass the error by using below check,

(typeof fn === "function") - return true only if it's function

const previewObj = ((typeof SingularPlayer === "function") && SingularPlayer(iframeId));

To avoid compile error, recommending to download the script and refer from local

Kannan G
  • 974
  • 6
  • 9
0

If 'SingularPlayer' isn't defined, it's not being included in the JS.

You'll need to import the singularplayer.js file in the JS files you want to access it from.

import SingularPlayer from '{url}/singularplayer.js'

You can't import a module from a URL in ES6, if you have to include the JS from another host, follow this questions answers: https://stackoverflow.com/a/44877953/4953804

hkjb
  • 19
  • 1
  • 5
0

I had a similar problem - global constant that was not accessible even through window.ConstantName.

I solved it thanks to this comment that guided me to create a new proxy file. The comment on the first line is the magic that solves the error.

// eslint-disable-next-line no-undef
const ConstantNameProxy = ConstantName;
export default ConstantNameProxy;

Then when I need to use the constant, I import and use the proxy such as:

import ConstantNameProxy from './path/to/proxy/file';

console.log(ConstantNameProxy);

You could of course just add the comment before every line where you'd use the global constant or disable the error more globally on file level for example, but that'd be unnecessary amount of garbage around the real code and could hide other real errors.

Marian Dolinský
  • 3,312
  • 3
  • 15
  • 29