Home > Software engineering >  Svelte `node scripts/setupTypeScript.js` gives 'MODULE_NOT_FOUND' error
Svelte `node scripts/setupTypeScript.js` gives 'MODULE_NOT_FOUND' error

Time:07-03

I am in a Svelte project and I have an issue:

  • My files are showing no errors in VSCode, but when executing npm run dev -- all Typescript syntax are shown as error and the server won't start.

So I tried removing all node_modules and reinstalling them, but now when I run node scripts/setupTypeScript.js I have the following error:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'C:\(...)\my-app\scripts\setupTypeScript.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

I tried reinstalling TypeScript globally (npm install typescript@latest -g), but that didn't help. I didn't find anyone with the same issue...

-- Note that it was working perfectly fine, until I did something I don't recall (trying to write something but it was received as shortcut and tampered with my files - I couldn't find anything special in the diff from the git repo, except package.json some version (TS related) were upgraded.)

Edit

So apparently the ./scripts/ folder should exists and doesn't. So i recreated it :

mkdir tmp
cd tmp
npx degit sveltejs/template svelte-typescript-app
cp -r svelte-typescript-app/scripts ../
cd ../
rm -r tmp

But running it now gives me another error that I didn't have before:

> node scripts/setupTypeScript.js
node:internal/fs/utils:344
    throw err;
    ^

    at Object.renameSync (node:fs:980:3)
    at Object.<anonymous> (C:\(...)\my-app\scripts\setupTypeScript.js:44:4)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -4058,
  syscall: 'rename',
  code: 'ENOENT',
  path: 'C:\(...)\my-app\src\\main.js',
  dest: 'C:\(...)\my-app\src\\main.ts'
}

Actually it is trying to convert main.js into main.ts, but because I already run it, it doesn't work. So I renamed it back into main.js and it was successful, but now running npm run dev -- gives me an error about Error: Identifier 'sveltePreprocess' has already been declared, indeed, it appears twice in rollup.config.js, just removing the redundance solves the issue.

Conclusion

In the end I still have the syntax error popping up, and the scripts/ directory disapeared again. So I can tell you this:

  • If you have the error Cannot find module '(...)\scripts\setupTypeScript.js' that means that you already ran the script
  • You should not try to run it twice, it will just give you more problems
  • The scripts folder is deleted automatically after it is ran, that's why it is missing
  • This script is not magic and will not solve a random typescript error you may have.

➕ upvote if it helped you.

Let me know if I should publish the conclusion as an answer that I validate. (?)

CodePudding user response:

it looks as if this path (scripts/setupTypeScript.ts) doesn't exist. Are you running the node command in the folder that contain the scripts folder?

CodePudding user response:

So, for those in the same situation, here is the detailled explanation:

  • The setupTypeScript.js is not working because the directory scripts/ is missing.
  • If you already ran it, the script automatically deleted itself, and its folder. (reason why it's missing...)
  • Fact is, that you should not re-run the script setupTypeScript.js a second time, if you already ran it.
  • If you are not sure if you already ran it, check the file main.js/ts if it is named : main.ts the script setupTypeScript.js has probably already been run.
  • If you think you didn't execute the script setupTypeScript.js and you need to do it but don't have it, then proceed as follow :
# double check the commands before to execute them
mkdir tmp
cd tmp
# Download the template with the directory `scripts` inside
npx degit sveltejs/template svelte-typescript-app
# Copy the `scripts` dir into your project folder
cp -r svelte-typescript-app/scripts ../
cd ../
rm -r tmp

If your main is already named main.ts, it will throw an error (at Object.renameSync (node:fs:980:3)) then, rename it to main.js.
If you already ran the script, it will duplicate some lines in rollup.config.js, make sure : import sveltePreprocess from 'svelte-preprocess'; and import typescript from '@rollup/plugin-typescript'; are not duplicated.

Hope it helps.

  • Related