martpie / next-transpile-modules

Next.js plugin to transpile code from node_modules. Please see: https://github.com/martpie/next-transpile-modules/issues/291
MIT License
1.13k stars 85 forks source link

escalade: Module not found: Can't resolve 'fs' #269

Closed dmitry-sidorov closed 2 years ago

dmitry-sidorov commented 2 years ago

Are you trying to transpile a local package or an npm package? @alfalab/core-components

Describe the bug Hello! I'm trying to transpile package @alfalab/core-components and got this:

error - ./node_modules/escalade/sync/index.js:2:32
Module not found: Can't resolve 'fs'

Import trace for requested module:
./node_modules/next-transpile-modules/src/next-transpile-modules.js
./next.config.js
./src/helpers.ts
./src/components/layout/layout.tsx
./src/components/layout/index.ts
./src/pages/_app.tsx

Expected behavior build succeeded.

Setup

I use next config from issue https://github.com/core-ds/core-components/issues/22, but still not working.

My next.config.js looks like:

/** @type {import('next').NextConfig} */
const withPlugins = require('next-compose-plugins');
const withTM = require('next-transpile-modules')(['@alfalab/core-components'], { debug: true });

const nextConfig = {
  basePath: '/pin',
  output: 'standalone',
  reactStrictMode: true,
  async redirects() {
    return [
      {
        basePath: false,
        source: '/',
        destination: '/pin',
        permanent: false,
      },
    ]
  },
  swcMinify: true,
  sassOptions: {
    includePaths: ['./src'],
    prependData: `@import "~@styles/globals.scss";`,
  }
}

module.exports = withPlugins([withTM], nextConfig);

This error is caused by escalade package, which is in dependencies for next-transpile-modules

martpie commented 2 years ago

Can you post the full log with the debug flag enabled? :)

malt03 commented 2 years ago

I have same issue. The following is the log with the Debug flag enabled.

% yarn workspace @web/site start
yarn workspace v1.22.19
yarn run v1.22.19
$ next dev --port 3100
ready - started server on 0.0.0.0:3100, url: http://localhost:3100
next-transpile-modules - trying to resolve the following modules:
  - @mish/error-codes
next-transpile-modules - the following paths will get transpiled:
  - /Users/malt03/Documents/service/mish/mish-app/packages/error-codes
info  - automatically enabled Fast Refresh for 1 custom loader
error - ../../../node_modules/escalade/sync/index.js:2:32
Module not found: Can't resolve 'fs'

Import trace for requested module:
../../../node_modules/next-transpile-modules/src/next-transpile-modules.js
../../../node_modules/@web/next-config/lib/cjs/next-config.js
../../../node_modules/@web/next-config/lib/cjs/index.js
./src/pages/_app.page.tsx

https://nextjs.org/docs/messages/module-not-found
info  - automatically enabled Fast Refresh for 1 custom loader
wait  - compiling /_error (client and server)...
error - ../../../node_modules/escalade/sync/index.js:2:32
Module not found: Can't resolve 'fs'

Import trace for requested module:
../../../node_modules/next-transpile-modules/src/next-transpile-modules.js
../../../node_modules/@web/next-config/lib/cjs/next-config.js
../../../node_modules/@web/next-config/lib/cjs/index.js
./src/pages/_app.page.tsx

https://nextjs.org/docs/messages/module-not-found
martpie commented 2 years ago

Are you running your apps in a specific JS runtime environment? Like Bun or Vercel Edge?

dmitry-sidorov commented 2 years ago

I fixed this issue for me.. randomly)) I have helpers.ts file in src directory. There is an import in this file: import { basePath } from '../next.config';

So, I have silent typescript error: Module '"../next.config"' has no exported member 'basePath'.ts

There is no error in node console, eslint tells everything is fine. But when I fixed it, 'fs' error goes away and everything works.

@malt03 Check typescript errors with IDE in your project, I guess. No idea why my ts error was silent.

malt03 commented 2 years ago

After trying a number of things, it appears that the problem is specific to my project. My problem has not been resolved, but you can close this Issue.

Thanks!