如何在 Linux Mint 上本地提供我的 javascript 应用程序

How do I serve my javascript app locally on Linux Mint

我目前正在学习教程,并且我已确保遵循每一个步骤。我正在尝试在 localhost:3000 本地托管我的 javascript 应用程序。但是,我无法这样做,每当我尝试 运行 npm run dev 时,我都会收到以下错误日志:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'run', 'dev' ]
2 info using npm@6.14.4
3 info using node@v10.19.0
4 verbose run-script [ 'predev', 'dev', 'postdev' ]
5 info lifecycle personalwebsite@0.0.0~predev: personalwebsite@0.0.0
6 info lifecycle personalwebsite@0.0.0~dev: personalwebsite@0.0.0
7 verbose lifecycle personalwebsite@0.0.0~dev: unsafe-perm in lifecycle true
8 verbose lifecycle personalwebsite@0.0.0~dev: PATH: /usr/share/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/omar-backup/Desktop/MrBoogle.github.io/personalWebsite/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
9 verbose lifecycle personalwebsite@0.0.0~dev: CWD: /home/omar-backup/Desktop/MrBoogle.github.io/personalWebsite
10 silly lifecycle personalwebsite@0.0.0~dev: Args: [ '-c', 'vite' ]
11 silly lifecycle personalwebsite@0.0.0~dev: Returned: code: 1  signal: null
12 info lifecycle personalwebsite@0.0.0~dev: Failed to exec dev script
13 verbose stack Error: personalwebsite@0.0.0 dev: `vite`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/share/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:198:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/share/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:198:13)
13 verbose stack     at maybeClose (internal/child_process.js:982:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid personalwebsite@0.0.0
15 verbose cwd /home/omar-backup/Desktop/MrBoogle.github.io/personalWebsite
16 verbose Linux 5.4.0-26-generic
17 verbose argv "/usr/bin/node" "/usr/bin/npm" "run" "dev"
18 verbose node v10.19.0
19 verbose npm  v6.14.4
20 error code ELIFECYCLE
21 error errno 1
22 error personalwebsite@0.0.0 dev: `vite`
22 error Exit status 1
23 error Failed at the personalwebsite@0.0.0 dev script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

我已经花了大约一个小时试图解决这个问题,但还没有找到任何东西。我安装了 vite 但没有托管(我正在尝试在 localhost:3000 上托管我的应用程序),而是使用 qt5ct 弹出一个奇怪的 window。不幸的是,这不再有效,因为我可能在尝试让它工作时搞砸了一些事情。 我正在学习的教程可以在 here. I'm running into issues around the 2:45 标记处找到。

When you create a project with npm init, like on the tutorial you've seen, a project from a template with be created at some directory. On your case, you 运行:

npm init @vitejs/app

This command got you a folder with some predefined template of Node.JS, which created a package.json for you, wich have all the dependencies required for that project to being able to work. At your case, for example:

{
  "name": "vite-project",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview"
  },
  "devDependencies": {
    "vite": "^2.3.3"
  }
}

As you can see, there is a devDependency call vite, which need to be installed to execute the npm script of dev, which t运行slates to "dev": "vite".

When you 运行 before, the vite dependencie wasnt installed on the project, and that was the reason of the error:

error personalwebsite@0.0.0 dev: `vite`

When you 运行 npm install, it installed all the required dependencies of package.json. So after you installed the vite dependency with npm install (which will take the package.json definition), it stopped lacking dependencies.

That is why your error change to:

 Cannot find module 'worker_threads' 

This is other error, and is probably because you are using and old version of node (info using node@v10.19.0). Trying to replicate I encounter the same problem:

But when I update to a newer version (Node 14), it didn't happend. So, just update your node version