Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wscourge opened this issue Feb 6, 2022 · 4 comments
Open

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

wscourge opened this issue Feb 6, 2022 · 4 comments

Comments

@wscourge
Copy link

wscourge commented Feb 6, 2022

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');
@tinesoft
Copy link

tinesoft commented Dec 9, 2022

Any update on this?

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

@wscourge
Copy link
Author

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

@Antonio225t
Copy link

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
Copy link

Antonio225t commented Mar 12, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants