Trouble using `require` with `rollup-plugin-commonjs` -- use TS `import` syntax instead
See original GitHub issueconfig
// 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:
- Created 6 years ago
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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).