首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用汇总导入公共is中的npm模块:“未定义要求”

无法使用汇总导入公共is中的npm模块:“未定义要求”
EN

Stack Overflow用户
提问于 2017-09-09 13:07:20
回答 1查看 5.1K关注 0票数 7

我在一个ES6项目上工作,我使用rollup和babel传输这个项目。它工作得很好,除非我试图导入使用公共is (特别是“某事”)的npm模块,在我的浏览器中获得一个错误"require is defined“(这意味着它还没有正确地将节点模块从commonjs编译到ES5)。但是,我使用了rollup插件-节点-解析和rollup plugin-commonjs,如果我理解得很好的话,它应该可以完成这个任务……

下面是我的rollup配置文件:

代码语言:javascript
复制
import babel from 'rollup-plugin-babel';
import eslint from 'rollup-plugin-eslint';
import resolve from 'rollup-plugin-node-resolve'; // to import node_modules packages easily
import commonjs from 'rollup-plugin-commonjs'; // convert commonjs to es6 (in case you use require)

export default {
  input: 'src/main.js',
  output: {
      file:'build/index.js',
      format: 'iife'
  },
  sourcemap: 'inline',
  plugins: [
    resolve({
      jsnext: true,
      main: true,
      browser: true
    }),
    commonjs({
        include: 'src/**'
    }),
    eslint({
      exclude: [
        'src/styles/**',
      ]
    }),
    babel({
      exclude: 'node_modules/**',
    })
  ],
};

我的babel配置文件:

代码语言:javascript
复制
{
    "presets": [
        [
           "es2015",
           {
                "modules": false
            }
        ]
    ],
    "plugins": ["external-helpers"]
}

我无法加载的模块有math.js、nsolvejs、chroma.js、data.gui等。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-10 15:12:23

这个问题可能与commonjs插件有关,它用于在构建时将cjs转换为es模块,因此您应该包括来自node_modules的cjs模块,而不是src。

代码语言:javascript
复制
 commonjs({
    include: 'node_modules/**'
 })
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46131026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档