IIFE. TypeError: require(…)(…) is not a function

后端 未结 1 1407
无人及你
无人及你 2021-01-15 14:12

Running simple script. Got an error.

const fetch = require(\"node-fetch\")
const url = \"https://www.someurl.com\"

(async ()=>{
    con         


        
相关标签:
1条回答
  • 2021-01-15 14:51

    Automatic Semicolon Insertion(ASI) doesn't work as you expect it to in some cases.

    IIFEs fall into one of those cases, where the parentheses are concatenated with previous line code.

    To ameliorate this, just prefix your IIFE's with a semicolon:

    const fetch = require("node-fetch")
    const url = "https://www.someurl.com"
    
    ;(async () => {
        const response = await fetch(url)
        console.log(response)
    })()
    

    Or as @estus suggests in the comments, just avoid writing semicolon-less code.

    0 讨论(0)
提交回复
热议问题