最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't import npm modules in commonjs with rollup : "require is not defined" - Stack Overf

programmeradmin3浏览0评论

I work on an ES6 project that I transpile using rollup and babel. It works well except when I try to import npm modules that use monjs (and particularly require('something')) getting an error "require is not defined" in my browser (which means it hasn't properly piled node modules from monjs to ES5). However, I use rollup-plugin-node-resolve and rollup-plugin-monjs, that should do that job if I've understood properly...

Here are my rollup config file:

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 monjs from 'rollup-plugin-monjs'; // convert monjs 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
    }),
    monjs({
        include: 'src/**'
    }),
    eslint({
      exclude: [
        'src/styles/**',
      ]
    }),
    babel({
      exclude: 'node_modules/**',
    })
  ],
};

and my babel config file:

{
    "presets": [
        [
           "es2015",
           {
                "modules": false
            }
        ]
    ],
    "plugins": ["external-helpers"]
}

Examples of modules that I can't load are math.js, nsolvejs, chroma.js, data.gui, etc.

I work on an ES6 project that I transpile using rollup and babel. It works well except when I try to import npm modules that use monjs (and particularly require('something')) getting an error "require is not defined" in my browser (which means it hasn't properly piled node modules from monjs to ES5). However, I use rollup-plugin-node-resolve and rollup-plugin-monjs, that should do that job if I've understood properly...

Here are my rollup config file:

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 monjs from 'rollup-plugin-monjs'; // convert monjs 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
    }),
    monjs({
        include: 'src/**'
    }),
    eslint({
      exclude: [
        'src/styles/**',
      ]
    }),
    babel({
      exclude: 'node_modules/**',
    })
  ],
};

and my babel config file:

{
    "presets": [
        [
           "es2015",
           {
                "modules": false
            }
        ]
    ],
    "plugins": ["external-helpers"]
}

Examples of modules that I can't load are math.js, nsolvejs, chroma.js, data.gui, etc.

Share Improve this question asked Sep 9, 2017 at 13:07 Arthur PesahArthur Pesah 2452 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

The issue is probably with monjs plugin, it is used to transform cjs into es modules at build time, therefore you should include the cjs modules from node_modules instead of src.

 monjs({
    include: 'node_modules/**'
 })
发布评论

评论列表(0)

  1. 暂无评论