markdown-toc icon indicating copy to clipboard operation
markdown-toc copied to clipboard

Error: Cannot statically analyse 'require(…, …)' in line 16

Open wscourge opened this issue 2 years ago • 4 comments

See: https://stackoverflow.com/questions/69898244/cannot-statically-analyse-require-in-line-16

Problem:

When importing markdown-toc in nextjs I am getting the issue. /pages/index.js

import toc from "markdown-toc";

Then I got the below error

./node_modules/markdown-toc/lib/utils.js Cannot statically analyse 'require(…, …)' in line 16

Why:

require gets overwritten by the utils assignment here:

'use strict';

/**
 * Module dependencies
 */

var diacritics = require('diacritics-map');
var utils = require('lazy-cache')(require);
var fn = require;
require = utils;

/**
 * Lazily required module dependencies
 */

require('concat-stream', 'concat');

wscourge avatar Feb 06 '22 06:02 wscourge

Any update on this?

I'm having the same error when importing the library in an Angular 14 application...

tinesoft avatar Dec 09 '22 15:12 tinesoft

I don't believe there will be, there are > 50 open issues and no update in the last 5 years.

wscourge avatar Jan 16 '23 05:01 wscourge

This fixed the problem but maybe you have to mess with dependencies...

Copy the library and put it in another folder to edit the source of it, then remove this:

var fn = require;
require = utils;

And replace all the require(..., ...) with utils(..., ...) like this:

utils('concat-stream', 'concat');
utils('gray-matter', 'matter');
utils('list-item', 'li');
utils('markdown-link', 'mdlink');
utils('minimist');
utils('mixin-deep', 'merge');
utils('object.pick', 'pick');
utils('remarkable', 'Remarkable');
utils('repeat-string', 'repeat');
utils('strip-color');

Hope it helps!

Antonio225t avatar Mar 12 '23 14:03 Antonio225t

Ok, I think I have a better solution. Try this: "markdown-toc-unlazy" https://www.npmjs.com/package/markdown-toc-unlazy And if you don't need to work with cli you can delete the file cli.js and in the libs/utils.js file remove this line at the start: var concat = require('concat-stream');.

Hope this helped you!

Antonio225t avatar Mar 12 '23 15:03 Antonio225t