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

🐛Uncaught Error: Cannot find module '21' #879

Closed
ghost opened this issue Feb 21, 2018 · 51 comments
Closed

🐛Uncaught Error: Cannot find module '21' #879

ghost opened this issue Feb 21, 2018 · 51 comments

Comments

@ghost
Copy link

ghost commented Feb 21, 2018

🎛 Configuration (.babelrc, package.json, cli command)

.babelrc:

{
  "presets": ["env"],
  "plugins": [
    ["babel-plugin-root-import", {
      "rootPathSuffix": "js",
      "rootPathPrefix": "@"
    }]
  ]
}

package.json:

{
  "name": "hwm-parcel",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "makmm",
  "license": "MIT",
  "dependencies": {
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-plugin-root-import": "^5.1.0"
  }
}

🤔 Expected Behavior

Run $ parcel index.html, go to localhost:1234, change something in the code and the browser should get the new code and load it

😯 Current Behavior

Run $ parcel index.html, go to localhost:1234, console shows correct sourcemaps:
image

change something in the code and this error comes up:
image

Refresh, now sourcemaps are broken:
image

💁 Possible Solution

No idea! <.<

🌍 Your Environment

Software Version(s)
Parcel 1.6.2
Node 9.5.0
npm 5.6.0
Operating System Debian sid
@DeMoorJasper
Copy link
Member

Could u provide us with a test repository?

@ghost
Copy link
Author

ghost commented Feb 21, 2018

@DeMoorJasper https://github.com/makmm/test-repo

@DeMoorJasper
Copy link
Member

Tried out your test repo and can confirm this is indeed happening, tried cleaning cache, disabling cache, disabling sourcemaps and going down to parcel v1.5.1 to see if it was not caused by recent changes.

@davidnagli
Copy link
Contributor

davidnagli commented Feb 22, 2018

Cannot find module ‘[number]’

I’ve seen this error message tons of times:
#198
#326
#666
#497

From looking at the issues the bugs seem totally unrelated, but I suppose maybe we should figure out where that error message is coming from and make it more informative. Or maybe the issues are actually even caused by some common bug... not sure 🤷‍♂️

@dima-takoy-zz
Copy link

It will be good if we provide file file mapping moduleId => moduleName

@ghost
Copy link
Author

ghost commented Feb 22, 2018

If there's a workaround I can use, please tell me as this is holding me back from developing my project.

@yufengwang
Copy link

yufengwang commented Feb 23, 2018

@Makmm fall back your node version to 8.9.4 and have a try

@ghost
Copy link
Author

ghost commented Feb 23, 2018

@yufengwang Tried with 8.9.3, same issue

@davidnagli
Copy link
Contributor

What OS is everybody who sees the issue using?

I’m on MacOS High Sierra (10.13.2), and I don’t get that Cannot find module error message. I only get:

Uncaught TypeError: Cannot read property 'getState' of undefined

Which makes sense, because when I log out stores I get:

stores: [undefined, GroupStore]

I’m not familiar with Angular so I have no clue why it’s undefined, but that doesn’t look like it’s being caused by Parcel.

Am I the only one that’s not getting the Cannot find module message?

@DeMoorJasper
Copy link
Member

@davidnagli i'm also using High Sierra

@ghost
Copy link
Author

ghost commented Feb 23, 2018

@davidnagli 1. not Angular 2. yes, the error is normal, I want to fix it but can't because of this issue

@davidnagli
Copy link
Contributor

@DeMoorJasper That’s really strange... I’ve tried a million times and I can’t reproduce. I’ve tried different browsers, different Node versions (tried both v9.6.1 and v8.9.4), different Parcel versions (1.6.2 and master), and even different terminals (Mac Terminal and iTerm2).

I’m pretty much at a loss for reproducing this.

Can you guys try to remove parcel and reinstall it? That’s the only thing I can think off... 😕

@ghost
Copy link
Author

ghost commented Feb 24, 2018

@davidnagli Tried with 9.6.1, npm scripts (npm run dev) AND global (parcel index.html) and still same issue, got the number 29 with npm scripts tho.

@davidnagli
Copy link
Contributor

davidnagli commented Feb 24, 2018

As a last ditch effort I just tried to perfectly recreate everything and installed the exact Node version number as @Makmm, and verified that npm version numbers also match. Then I ran Parcel without HMR, cache, or sourcemaps, and I still didn’t get the error!

> parcel --version
1.6.2
> npm --version
5.6.0
> node --version
v9.5.0
> parcel index.html --no-hmr --no-cache --no-source-maps
Server running at http://localhost:1234
✨  Built in 9.15s.

Then I opened localhost:1234 in Chrome (64.0.3282.167 (Official Build) (64-bit)), turned on Verbose logs, and disabled Group Similar (I think it’s a new feature). Still nothing...

I also tried in Safari, with the same exact result.

Let’s see if anyone else is able to reproduce...

@ghost
Copy link
Author

ghost commented Feb 24, 2018

@davidnagli Reproduced on a completly different machine just now, Debian sid too, node 8.9.4, latest parcel, deleted node_modules and re-installed (I pushed node_modules, whoops :P) and same error, but with 39 now. :/

EDIT: tried with the exact same version of Google Chrome, same issue >.>

@ghost
Copy link
Author

ghost commented Feb 24, 2018

Wait @davidnagli , did you try modifying a file? that's when the error comes up.

@davidnagli
Copy link
Contributor

Oh!!! You mean during HMR?!

@ghost
Copy link
Author

ghost commented Feb 24, 2018

I don't know what HMR is. >.<

@davidnagli
Copy link
Contributor

davidnagli commented Feb 24, 2018

HMR is pretty much the “live reload” thing that refreshes your code within your app automatically whenever you make a change. See: HMR Docs

So you’re saying that you don’t see an error when you open localhost:1234 the first time, but then once you make a change and the app get’s automatically refreshed your seeing that error?

Try running parcel with these flags so we can narrow it down:

parcel index.html --no-hmr --no-cache --no-source-maps

@ghost
Copy link
Author

ghost commented Feb 24, 2018

@davidnagli HMR is failing (in the way you said it is), but source maps are too, if I run it with the --no-hmr flag only, I have the same source map issue after changing a file

@davidnagli
Copy link
Contributor

Interesting...

@davidnagli
Copy link
Contributor

Ummm question:

What does @/ do here?

import NavBar from '@/components/NavBar'
import GlobalAlert from '@/components/GlobalAlert'
import RouterView from '@/core/router/RouterView'

@ghost
Copy link
Author

ghost commented Feb 24, 2018

Check .babelrc; it's a way to say go to the root of the code

@davidnagli
Copy link
Contributor

davidnagli commented Feb 24, 2018

Ok I’m pretty sure that’s gonna cause issues, because Parcel only runs Babel after resolving imports, meaning that those paths won’t be transformed with babel before we try to resolve them.

Can you try temporarly getting rid of them and seeing if it magically solves the issue?

Just so you know, we just added support for project relative imports built into Parcel, so you won’t even need this whole babel set up soon. The PR has already been merged and should be released to npm pretty soon :)

The PR: #850

I’m fairly certain that if you change those paths to be relative paths, it should fix the error 👍

@ghost
Copy link
Author

ghost commented Feb 24, 2018

Oh great! let me try that, one second

@ghost
Copy link
Author

ghost commented Feb 24, 2018

Nope, doesn't fix it ://

@davidnagli
Copy link
Contributor

😡 😠 😡

@mischnic
Copy link
Member

mischnic commented Feb 25, 2018

Commenting out the css files, this is the error on HMR reload:

Uncaught TypeError: Cannot read property 'modules' of undefined
    at hmrApply (initialize.js:22)
    at initialize.js:22
    at Array.forEach (<anonymous>)
    at WebSocket.ws.onmessage (initialize.js:22)

@mischnic
Copy link
Member

@Makmm Looks like it.

yes, the error is normal, I want to fix it but can't because of this issue

Ironically, parcel doesn't work correctly because of the error.

@mischnic
Copy link
Member

mischnic commented Feb 26, 2018

@DeMoorJasper @devongovett
I think I found the cause:

for (var i = 0; i < entry.length; i++) {
newRequire(entry[i]);
}
// Override the current require with this new one
return newRequire;
})

newRequire runs the childBundles on page load: first the HMR loader and then the code itself.
When an error occurs on the inital load inside a childBundle, an error is thrown and newRequire doesn't get returned. Therefore the global window.require is undefined
// eslint-disable-next-line no-global-assign
require = (function (modules, cache, entry) {
// Save the require from previous bundle to this closure if any

and global.require will be undefined in the HMR loader as well:
data.assets.forEach(function (asset) {
if (!asset.isNew) {
hmrAccept(global.require, asset.id);
}
});

"Proof"/ Bugfix(?):

  for (var i = 0; i < entry.length; i++) {
    try{
      newRequire(entry[i]);
    } catch(e){
      console.error(e)
    }    
  }

works!
bildschirmfoto 2018-02-26 um 15 27 52
(The first error is from the added console.error and the second from HMR)

@DeMoorJasper
Copy link
Member

@mischnic Feel free to make a PR :)

@willin
Copy link

willin commented Mar 16, 2018

css-loader.js:30 Uncaught (in promise) Error: Cannot find module '3b96d1e50931156f07cc051bc060909f.svg,119'
    at newRequire (css-loader.js:30)
    at newRequire (css-loader.js:22)
    at localRequire (css-loader.js:30)
    at css-loader.js:30
newRequire @ css-loader.js:30
newRequire @ css-loader.js:22
localRequire @ css-loader.js:30
(anonymous) @ css-loader.js:30
Promise.then (async)
loadBundles @ css-loader.js:30
require.0 @ css-loader.js:30
newRequire @ css-loader.js:30
require.17 @ css-loader.js:30
(anonymous) @ css-loader.js:30

@luikore
Copy link
Contributor

luikore commented May 7, 2018

When I import an image from js in a single file vue component, it shows Uncaught Error: Cannot find module '467' too.

It took me a lot of time to realize that I should config babel to enable importing images.
After adding babel-plugin-import-static-files the error goes away.

I think a more friendly message should be something like "foo.png" is not a valid javascript file or importing "foo.png" throws an error so I know how I can fix it easier.

@DeMoorJasper
Copy link
Member

@luikore requiring images should work, it should return the url of the asset.
I agree that the errors should represent the filenames instead of the id

Sent with GitHawk

@luikore
Copy link
Contributor

luikore commented May 7, 2018

@DeMoorJasper Thanks, your are right. I made some tests and confirmed loading images in js is supported out of the box.

After some more tries, I found that the real cause of the "Can not find module" error is
loading the same image in both HTML and JS. A minimal example to recur this error:

(assume there is a "foo.png" under the same folder:

package.json

{
  "scripts": {
    "dev": "parcel index.html"
  },
  "devDependencies": {
    "parcel-bundler": "^1.8.1"
  },
  "dependencies": {
  }
}

index.html

<html>
<head></head>
<body>
    <h1>img from html</h1>
    <img src="./foo.png">
    <h1>img from js</h1>
    <img id="from-js">
    <script src="./app.js"></script>
    <style>img {border: 1px solid black; width: 100px; height: 100px;}</style>
</body>
</html>

app.js

import foo from './foo.png'
document.querySelector('#from-js').src = foo

Then yarn && yarn dev and open http://localhost:1234/index.html to see the error.

(EDIT: reduced dependencies to show this is a parcel issue)

@xeoneux
Copy link

xeoneux commented May 8, 2018

@luikore I spent good amount of time figuring it out myself. Can't import the same asset twice I guess.

@tcodes0
Copy link

tcodes0 commented Jun 7, 2018

I ran into this issue today. I had JS importing the asset import foo from "..." and later on css using the same asset as a bg image url(....). I tried removing .cache and renaming the asset, but it didn't work. I had to duplicate it with a different name so JS imports one, and css the other. Can I help to get it fixed? 🔥🐛

@ghost
Copy link

ghost commented Jun 18, 2018

Can I help to get it fixed?

Yes, please isolate the root cause and sub a pull to fix all related issues.

@pselden
Copy link
Contributor

pselden commented Jul 8, 2018

If someone else encounters this, same thing happens if you have the same image in the index.html and also imported from your JS.

@paleite
Copy link

paleite commented Jul 12, 2018

@DeMoorJasper : It seems parcel won't resolve the module correctly if it's been referenced in a non-JS file previously.

I don't know if this helps or not, but I found a workaround, which might be a hint into solving the issue.

Not working

<body>
  <img src="./image.png" alt="">
  <script src="main.js"></script>
</body>

Working

<body>
  <script src="main.js"></script>
  <img src="./image.png" alt="">
</body>

Hope this helps!

@lourd
Copy link

lourd commented Jul 12, 2018

I've expanded the basic html integration test to demonstrate the bug and am working on debugging it now. Any collaboration or ideas welcome.

@mischnic
Copy link
Member

mischnic commented Sep 30, 2018

@lourd So should this fail ? It doesn't (anymore) with 1.10.1

<body>
  <img src="./image.png">
  <img id="from-js">
  <script src="./index.js"></script>
</body>
import foo from './image.png';
document.querySelector('#from-js').src = foo;

@lourd
Copy link

lourd commented Oct 2, 2018

Nice! I tested my reproduction against the latest on master and it looks like it's passing now. I'm happy to make a PR if that test is helpful. Either way, I think this issue can be closed.

@mischnic
Copy link
Member

mischnic commented Oct 3, 2018

Either way, I think this issue can be closed.

The issue in #898 should still cause this error.

@DeMoorJasper
Copy link
Member

@lourd feel free to open a PR if there isn't a test for it yet. The more coverage the better.

Gonna close this as it appears to have been fixed.

@mikolysz
Copy link

mikolysz commented Jan 8, 2019

I still have that issue. Here's an example:

index.html:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta
			name="viewport" 
			content="width=device-width, initial-scale=1, shrink-to-fit=no"
		/>
		<Title>Sample hooks app</title>
	</head>
	<body>
		<div id="root"></div>
		<script src="./src/index.js"></script>
	</body>
</html>

index.js

import React from 'react';
import ReactDOM from 'react-dom';

const App = () => <div>Hey</div>;
ReactDOM.render(<App />, document.getElementById("toor")); // error here

package.json (notice React alpha, not sure if it matters or not):

{
  "name": "hooks-parcel-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "parcel-bundler": "^1.11.0",
    "react": "^16.7.0-alpha.2",
    "react-dom": "^16.7.0-alpha.2"
  }
}

Steps to reproduce:

  1. Prepare an app with the files included above.
  2. Try launching it, you should get the right error.
  3. Change toor to root in the index.js, instead of working, you will get:
    index.js:5 Console was cleared
    Error
    index.js:5 Uncaught TypeError: Cannot read property 'modules' of undefined
        at hmrApply (index.js:5)
        at index.js:5
        at Array.forEach (<anonymous>)
        at WebSocket.ws.onmessage (index.js:5)
  4. refresh the page, it will work.
  5. Change to toor, toora, toraaaaaa etc. It will give you right errors.
  6. Change back to root, it will work.

Conclusion:

If the app is loaded with an error for the first time, the error is displayed but everything crashes on an edit, no matter if the edit would make the run successful or not. If the app loads successfully for the first time, everything is okay, even if there's an error later on, you can fix it and the app will continue working.

@mischnic
Copy link
Member

mischnic commented Jan 8, 2019

If the app is loaded with an error for the first time, the error is displayed but everything crashes on an edit, no matter if the edit would make the run successful or not.

This is exactly what I attempted to fix in #898 (didn't get merged).

An even shorter sample:

throw new Error("STOP!");

console.log("print");

@mischnic mischnic reopened this Jan 8, 2019
@mikolysz
Copy link

mikolysz commented Jan 8, 2019

@mischnic why didn't it get merged? Is there any chance for it to be fixed in the near future? This looks serious to me and i'm surprised that such a mature project has such an error and that no one has noticed it earlier.

@mischnic
Copy link
Member

mischnic commented Jan 8, 2019

why didn't it get merged? Is there any chance for it to be fixed in the near future?

There were some issues with my fix. I might take another look.

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

Successfully merging a pull request may close this issue.