简体   繁体   中英

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?

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.

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

您是否尝试使用重新构建或类似的东西或 npm cache clean insted of npm --force cache clear?

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.

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.

对我来说清楚地捕捉我的浏览器它有效

Fixed by Pressing Ctrl+s !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM