react-monaco-editor icon indicating copy to clipboard operation
react-monaco-editor copied to clipboard

Error: cannot find 'monaco-editor'

Open IrfanBaqui opened this issue 8 years ago • 18 comments

I've been getting the this when I try to use this package (latest with monaco-editor v0.13.1):

Here's my webpack build:

const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = {
  mode:'development',
  devtool: 'source-map',
  optimization: {
    splitChunks:{}
  },
  entry: {
    app: './ui/client/index.js'
  },
  context: path.resolve(__dirname),
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].js',
    publicPath: '/dist/'
  },
  plugins: [
    new ExtractTextPlugin('styles.css'),
    new webpack.DefinePlugin({
      "process.env": {
        BROWSER: JSON.stringify(true)
      }
    }),
    new MonacoWebpackPlugin()
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        include: path.resolve(__dirname),
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' })
      }
    ]
  }
};

Any idea why? I'd appreaciate your response!

IrfanBaqui avatar Jun 17 '18 17:06 IrfanBaqui

@IrfanBaqui I reckon you didn't post our original error

expobrain avatar Jun 17 '18 21:06 expobrain

Hey @expobrain, sorry I should've included it initially. Here's the full error:

Error: Cannot find module 'monaco-editor'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/irfanbaqui/ia-app/node_modules/react-monaco-editor/lib/editor.js:11:21)
    at Module._compile (module.js:652:30)
    at Module._compile (/Users/irfanbaqui/ia-app/node_modules/pirates/lib/index.js:91:24)
    at Module._extensions..js (module.js:663:10)
    at Object.newLoader [as .js] (/Users/irfanbaqui/ia-app/node_modules/pirates/lib/index.js:96:7)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/irfanbaqui/ia-app/node_modules/react-monaco-editor/lib/index.js:8:15)
    at Module._compile (module.js:652:30)

IrfanBaqui avatar Jun 18 '18 00:06 IrfanBaqui

I ran into the same issue. :(

picodoth avatar Jun 27 '18 07:06 picodoth

I ran into the same issue too. :(

lavenleo avatar Jun 28 '18 13:06 lavenleo

I ran into pretty much the same thing with jest in test using this module,

 FAIL  __tests__/components/Layout.spec.js
  ● Test suite failed to run

    Cannot find module 'monaco-editor' from 'editor.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (node_modules/react-monaco-editor/lib/editor.js:11:21)

had to "alias" monaco-editor to react-monaco-editor to help jest resolve it:

...
"moduleNameMapper": {
  "^.+\\.(css|scss)$": "identity-obj-proxy",
  "monaco-editor": "<rootDir>/node_modules/react-monaco-editor"
},
...

sanusart avatar Jul 10 '18 20:07 sanusart

Ran into the same issue

brettgoulder avatar Jul 24 '18 20:07 brettgoulder

Ran into the same issue

irbisadm avatar Aug 01 '18 23:08 irbisadm

Does anyone have a viable fix for this issue? I'm starting to experiment with building a facade of monaco editor just to get my tests building... but there must be a better way!

TheRealPiotrP avatar Aug 02 '18 23:08 TheRealPiotrP

Ran into the same issue, but @sanusart solution proved effective, thanks!

LongBu avatar Aug 08 '18 02:08 LongBu

Ran into the same issue

yansenlei avatar Sep 13 '18 02:09 yansenlei

Are you guys doing some SSR ? I ran into the same issue, it was related to Next.js SSR. I solved it by ensuring react-monaco-editor was only loaded on the Client Side with NextJs Dynamic Import. Also I was able to solve it by requiring the module dynamically from CDM

emyann avatar Oct 06 '18 07:10 emyann

Is there something that we can do in this module or is it a problem with build systems in general?

domoritz avatar Oct 06 '18 15:10 domoritz

I think monaco-editor should be in the peer-dependencies and the app that consume this package should have monaco-editor in its dependencies to resolve this issue.

veeramarni avatar Oct 23 '18 19:10 veeramarni

@emyann can you show a gist of how you accomplished this? Thanks! (edit: looks like this is a good resource)

mattpardee avatar Nov 08 '18 16:11 mattpardee

@mattpardee I tried to use https://github.com/resourcesco/snippets/blob/master/docs/monaco-editor-with-next.md steps but it was not working well.

Here's my experiment (pretty early though) with NextJs and Monaco https://morphism-playground.now.sh/ (A playground for https://github.com/nobrainr/morphism)

Here's the code source: https://github.com/nobrainr/morphism-playground

  • Monaco Webpack Plugin: https://github.com/nobrainr/morphism-playground/blob/8ec477594d41e71e2640e87f8f522f84dfb3b28a/next.config.js#L7
  • Statically serve Monaco files with Express: https://github.com/nobrainr/morphism-playground/blob/20d38482594c7a006001df88d4309776601337b0/server/index.ts#L19
  • Wrapping React Monaco Editor in my Own component: https://github.com/nobrainr/morphism-playground/blob/8ec477594d41e71e2640e87f8f522f84dfb3b28a/src/components/Playground.tsx#L5

Hope it helps, let me know!

emyann avatar Nov 08 '18 17:11 emyann

I have spent a lot of time in the wrong direction trying to add babel es2015 preset and all other different stuff to make monaco-editor esm code work from jest.

The solution by @sanusart finally worked.

buybackoff avatar Mar 21 '19 15:03 buybackoff

I had also being struggled with this issue for couple of hours. I fixed it using following steps:

  1. Create __mocks__ directory in the projects root (at the same level with node-modules);
  2. Create monaco-editor.ts that exports modules you need:
module.exports = {
  editor: {
    getModelMarkers: jest.fn().mockReturnValue([]),
    // ...other methods here
  },
  Position: {}
  // ...other modules here
};

And that's it! When Jest parser finds imported 'monaco-editor' modules it looks in mocks folder first where finds our created 'monaco-editor.ts' module. Then it takes all the required modules from here.

CherpakovPhilipp avatar Sep 26 '22 20:09 CherpakovPhilipp

I'm using @monaco-editor/react package and I followed the suggestion above comment by @sanusart . Thanks

This works for me:

    "transformIgnorePatterns": [
      "/node_modules/(?!(monaco-editor)).+\\.js$"
    ],
    "moduleNameMapper": {
      "^monaco-editor$": "<rootDir>/node_modules/@monaco-editor/react"
    }

vishalsatam avatar Jan 17 '23 17:01 vishalsatam