【问题标题】:Nuxt - Node error when I want to import some packagesNuxt - 我想导入一些包时出现节点错误
【发布时间】:2021-06-23 10:30:02
【问题描述】:

我用的是 Nuxt,有时候想用一些 npm 包的时候,出现这个错误:

SyntaxError
Unexpected token '<'

堆栈:

vm.js:102:7
new Script
internal/modules/cjs/loader.js:1114:10
Module._extensions..js
internal/modules/cjs/loader.js:950:32
Module.load
internal/modules/cjs/loader.js:790:14
Module._load
internal/modules/cjs/loader.js:974:19
Module.require
webpack:/external "vue-typeahead-bootstrap":1:
Object.vue-typeahead-bootstrap
webpack/bootstrap:25:
__webpack_require__
pages/account/tabs/addresses.js:693:81
Module../node_modules/babel-loader/lib/index.js?!./node_modules/vue-loader/lib/index.js?!./components/address/form/country.vue?vue&type=script&lang=js&
webpack/bootstrap:25:

我在这个包上有这个错误:vue-typeahead-bootstrap

如果我导入包:

import VueTypeaheadBootstrap from ['vue-typeahead-bootstrap'](https://github.com/mattzollinhofer/vue-typeahead-bootstrap)

export default {
  components: { VueTypeaheadBootstrap },
}

它抛出错误。

是因为包不支持还是什么的?

【问题讨论】:

    标签: node.js nuxt.js yarnpkg


    【解决方案1】:

    您可以尝试转换它。 https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-build#transpile

    像这样添加包名

    {
      build: {
        transpile: [
          ({ isServer }) => 'vue-typeahead-bootstrap'
        ]
      }
    }
    

    在这里回答:https://github.com/mattzollinhofer/vue-typeahead-bootstrap/issues/19#issuecomment-645510809

    【讨论】:

    • 好吧,它似乎完成了这项工作!谢谢,我会阅读更多关于转译的内容
    • @VincentDecaux 转译是在您制作一些代码时,例如 ES6 与 commonJS 代码兼容。在这里,这个包可能只适用于 ES6,因此我们需要要求它被转译。
    • 感谢您的解释,现在我明白了。但是,不知道 nuxt / node 是否有一些缓存,因为它在某一时刻工作,然后不再
    猜你喜欢
    • 2019-12-09
    • 2016-01-23
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多