John Kealy
John Kealy

Reputation: 1883

Why does npm try to compile a old version of my code?

I'm totally new to npm and node.js so please forgive me if I'm not giving the right detail. I'm writing a plugin for a website that uses an out-of-the-box framework in npm. It was working fine as I coded away happily, but at some point it started trying to compile a file that no longer exists.

I use npm run start to compile my html file (which contains all the javascript code as well) and get this error:


ℹ info Compiler will compile ./src/plugin.html
ℹ info Transpiling with babel
 ✖ error Error
  SyntaxError: unknown: Unexpected token (555:3)

    553 | 
    554 | 
  > 555 | });
        |    ^

What breaks my brain is that this simple syntax error is not present in ./src/plugin.html (I've quadruple checked this fact). This obvously pertains to an earlier version of the file.

Does npm have some kind of cache that would cause it to compile an older version of a file? I've noticed that if I remove most of the code, it compiles again, but when I replace the code it breaks again. It seems that if the file is somewhat similar to its cached version, it tries to compile the cached version and not the current version.

I've tried clearing the cache with npm --force cache clear but to no effect.

Any suggestions?

Upvotes: 8

Views: 12008

Answers (8)

user9468014
user9468014

Reputation: 499

Try to delete .next folder it will delete the catched files

Upvotes: -1

Omid Raha
Omid Raha

Reputation: 10680

Fixed by Pressing Ctrl+s !

For me vsc by default doesn't save automatically changed code like PyCharm.

Upvotes: -1

Karzan Kamal
Karzan Kamal

Reputation: 1252

for me clear catching my browser it works

Upvotes: -1

Wahab Omoyele
Wahab Omoyele

Reputation: 23

I recently faced this issue. If you are using typescript in your project, you may have used a wrong configuration in your tsconfig.json file that may have built the typescript files into their corresponding js files and these js files are the ones that runs every time you run the project.

Upvotes: -1

vladCovaliov
vladCovaliov

Reputation: 4403

I had the same problem after a cancelled git rebase. The only way I could fix this was to change something in those files & revert the change after.

Upvotes: -1

David
David

Reputation: 4093

the only way I was able to get out of this scenario was:

  • rename the file
  • refactor the references to it, so it will compile
  • npm start
  • stop it
  • rename and refactor the file back to its original name

Upvotes: 3

BearCoder
BearCoder

Reputation: 34

Do you try to use rebuild or something like that or npm cache clean insted of npm --force cache clear?

Upvotes: -1

John Kealy
John Kealy

Reputation: 1883

For anyone who has a similar problem with compiling code with npm, I discovered that my problem was indeed a simple syntax error. But it seems that npm became confused about what the compile error actually was. It was pointing to a line of code that did not exist in the file, and I have no idea where it got this from.

The syntax error that was the culprit in this particular case was a missing }; to close a wrapped set of functions.

So it seems that the lesson here is not to put too much trust in npm's ability to understand what syntax errors are inciting a compile error.

Upvotes: 6

Related Questions