question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble using `require` with `rollup-plugin-commonjs` -- use TS `import` syntax instead

See original GitHub issue

config

// rollup.config.js
import typescript from 'rollup-plugin-typescript2';
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve'

export default {
  input: 'src/index.ts',
  output: {
    format: 'es',
    file: 'dist/index.js'
  },

  plugins: [
    typescript(),
    nodeResolve(),
    commonjs()
  ]
}

input

// index.ts
var f = require('untypedModule')
f()

output

// dist/index.js
var f = require('untypedModule')
f()

when typescript() removed from plugins array

// dist/index.js
function f() {...}
f()

As you can see ‘typescript-plugin’ somehow prevents ‘commonJs’ and ‘nodeResolve’ to produce proper output

If I remove ‘typescript-plugin’ from chain, the output is correct

Please help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
ezolenkocommented, Nov 16, 2017

Yeah, I think typescript doesn’t try to resolve requires, so they are invisible to rollup (because of "module": "es2015" option likely, which this plugin requires), but I’m not entirely sure.

1reaction
ezolenkocommented, Nov 16, 2017

Looks like you need "allowJs": true in tsconfig (that forces "declaration": false, beacuse ts can’t generate declarations when there are untyped modules in the mix). Then change require to import: import { helper } from './helpers'

Then rollup will generate correct bundle – an empty file – because you don’t explicitly export anything. Once you export stuff, for example export const x = helper(), the bundle will contain your helper (but only with the stuff you actually use, the rest rollup will shake from the tree).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't import npm modules in commonjs with rollup : "require is ...
I work on an ES6 project that I transpile using rollup and babel. It works well except when I try to ...
Read more >
@rollup/plugin-commonjs - npm
NodeJS where ES modules can only import a default export from a CommonJS dependency: // input const foo = require('foo'); // output import...
Read more >
How to Correctly Use TypeScript Module Import Syntax and ...
JavaScript with CommonJS can use “require()” to import other CommonJS modules, but can only use dynamic import “import()” to import ES Modules.
Read more >
rollup.js
For example, with CommonJS, the entire tool or library must be imported. // import the entire utils object with CommonJS const utils =...
Read more >
Module Bundling - Stencil.js
For Stencil to bundle your components most efficiently, you must declare a single component (class decorated with @Component ) per TypeScript file, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found