我正在VM内的一个项目上工作(确实是通过vscode远程容器在Windows上的docker)。我想避免在$(pwd)/node_modules
上构建我的应用程序,因为$(pwd)
下的所有内容都已同步到Windows文件系统,这会导致严重的性能问题。
总体上来说,我对Node.js还是很陌生,但是我很惊讶这似乎很难解决。
我已经成功地开始了:
yarn --modules-folder /tmp/vendor
这给了我一个/tmp/vendor
文件夹,其中列出了预期的依赖项。
下一步,我想运行开发服务器,所以我运行它:
yarn --modules-folder /tmp/vendor start
我得到/bin/sh: react-scripts: command not found
。我认为yarn
的{{1}}在运行命令(--modules-folder
调用yarn start
)时不会重新配置PATH信息。所以我只是添加了自己的PATH作为解决方法:
react-scripts start
但是现在我明白了:
PATH=$PATH:/tmp/vendor/.bin/ yarn --modules-folder /tmp/vendor start
由于它抱怨缺少模块,因此我也设置了NODE_PATH(我猜yarn也不会转发该变量):
[root@352b76226b83 owio]# PATH=$PATH:/tmp/vendor/.bin/ yarn --modules-folder /tmp/vendor start
yarn run v1.22.5
$ react-scripts start
internal/modules/cjs/loader.js:965
throw err;
^
Error: Cannot find module 'react-dev-utils/crossSpawn'
Require stack:
- /tmp/vendor/react-scripts/bin/react-scripts.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:962:15)
at Function.Module._load (internal/modules/cjs/loader.js:838:27)
at Module.require (internal/modules/cjs/loader.js:1022:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/tmp/vendor/react-scripts/bin/react-scripts.js:18:15)
at Module._compile (internal/modules/cjs/loader.js:1118:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1138:10)
at Module.load (internal/modules/cjs/loader.js:982:32)
at Function.Module._load (internal/modules/cjs/loader.js:875:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/tmp/vendor/react-scripts/bin/react-scripts.js' ]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command
有了这个,我得到:
NODE_PATH=/tmp/vendor PATH=$PATH:/tmp/vendor/.bin/ yarn --modules-folder /tmp/vendor start
我被困在这里,因为./src/index.jsx
Line 0: Parsing error: Cannot find module 'eslint-scope' from '/tmp/vendor/eslint/lib/api.js'
确实存在,而且我已经拥有解决路径问题的能力。
我很想尝试使用CRA和yarn之外的其他工具,但我对生态系统的了解还不够,还不了解如何将我的应用程序移植过来。任何建议将不胜感激。
编辑:
通过将eslint-scope
更改为vendor
,我已经走近了。我猜想这种惯例是有必要的。我还向node_modules
文件中添加了NODE_PATH=/tmp/node_modules
,并向.env
文件中添加了--modules-folder /tmp/node_modules
。
编辑2:
按照我的建议,我为.yarnrc
做了一个符号链接,并且这一次似乎可行,因为我已经向$(pwd)/node_modules -> /tmp/node_modules
添加了NODE_PATH。
似乎所有的react-script东西现在都可以正常工作,但是在.env
下编译本地源文件时,我遇到了以下错误:无法从src/components/
解析确实存在的模块。
答案 0 :(得分:0)
经过Estus的修补和支持后,我发现拥有以下功能对我有用:
.env
文件和NODE_PATH=/tmp/node_modules
.yarnrc
文件和--modules-folder /tmp/node_modules
$(pwd)/node_modules -> /tmp/node_modules
的符号链接我不确定为什么需要符号链接,并且很想知道是否有人有.env
这样的解决方案。似乎我的本地源文件都是针对此文件夹编译的,而不管是否设置了NODE_PATH
。
我不了解其他奇怪之处,但也许有人会:
在我的NODE_PATH
命令中,我不能将yarn
作为变量。 .env
中的值似乎改变了node_modules
中的模块的编译方式。
我不能使用node_modules
以外的名称。不知道为什么。
顺便说一句:
对于在此着陆的所有人,如果遇到相同的问题(Windows或Mac上的docker性能降低),则可能需要确保您的编辑器未索引node_modules。例如,我在files.watcherExclude
中将.vscode/settings.json
设置为包括"**/node_modules": true,"
。