[英]bazel rules_nodejs can't resolve modules using custom package.json location
Bazel rules_nodejs can't resolve modules using custom package.json location. Bazel rules_nodejs 无法使用自定义 package.json 位置解析模块。
Can someone help explain how to fix it?有人可以帮助解释如何解决它吗?
Ideally I'd like to use a single tsconfig.json
in third_party/npm
instead.理想情况下,我想在
third_party/npm
中使用单个tsconfig.json
。
bazel build //demo/node:bin
Gives error:给出错误:
demo/node/src/lib.ts(1,19): error TS2307: Cannot find module 'chalk' or its corresponding type declarations.
Gist: https://gist.github.com/sbussard/9110d1bdcc784ca0a9303d4393e82f49要点: https ://gist.github.com/sbussard/9110d1bdcc784ca0a9303d4393e82f49
Folder structure文件夹结构
(repo root)
↳ WORKSPACE
↳ third_party
↳ npm
↳ package.json
↳ yarn.lock
↳ demo
↳ node
↳ BUILD
↳ tsconfig.json
↳ src
↳ lib.ts
WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "2b2004784358655f334925e7eadc7ba80f701144363df949b3293e1ae7a2fb7b",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.4.0/rules_nodejs-5.4.0.tar.gz"],
)
load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_dependencies")
build_bazel_rules_nodejs_dependencies()
load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install")
node_repositories()
yarn_install(
name = "npm",
package_json = "//third_party/npm:package.json",
yarn_lock = "//third_party/npm:yarn.lock",
)
BUILD
load("@npm//@bazel/typescript:index.bzl", "ts_project")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
ts_project(
name = "lib",
srcs = glob(["src/*.ts"]),
deps = [
"@npm//@types/chalk",
"@npm//@types/node",
"@npm//chalk",
],
)
nodejs_binary(
name = "bin",
data = [":lib"],
entry_point = "src/lib.js",
)
lib.ts
import chalk from 'chalk';
const { blue, red, green, yellow } = chalk;
const colors = [blue, red, yellow, blue, green, red];
console.log(
'Google'
.split('')
.map((c, i) => colors[i % colors.length](c))
.join('')
);
package.json (inside dependencies)
"@types/chalk": "^2.2.0",
"chalk": "^5.0.1",
tsconfig.json
{
"include": ["src", "types"],
"compilerOptions": {
"module": "es2020",
"target": "esnext",
"moduleResolution": "node",
"baseUrl": "./",
"rootDir": "./",
"paths": {
"src/*": ["src/*"]
},
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
"importsNotUsedAsValues": "error"
}
}
chalk
package chalk
包Run跑
npm uninstall chalk
and和
npm install chalk@"<5"
to down grade the chalk
package.降低
chalk
包的等级。
BUILD
file BUILD
文件Remove this line "@npm//@types/chalk",
under ts_project -> deps -> []在 ts_project -> deps -> [] 下删除这一行
"@npm//@types/chalk",
chalk
package issue chalk
包问题I have faced the same issue with my Webpack setup.我的 Webpack 设置也遇到了同样的问题。
This was a problem caused by TypeScript and chalk
.这是由 TypeScript 和
chalk
引起的问题。
From NPM: chalk
:来自NPM:
chalk
:
IMPORTANT: Chalk 5 is ESM.
重要提示:粉笔 5 是 ESM。 If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now.
如果您想将 Chalk 与 TypeScript 或构建工具一起使用,您现在可能想要使用 Chalk 4。 Read more.
阅读更多。
Following the above warning, we can find that根据上述警告,我们可以发现
You are currently using:您当前正在使用:
"chalk": "^5.0.1",
"粉笔": "^5.0.1",
Please re-install and replace it:请重新安装并更换它:
"chalk": "<5",
And here is an example NPM script:这是一个示例 NPM 脚本:
npm uninstall chalk
npm install chalk@"<5"
Currently, the latest version smaller than 5 is v4.1.2
.目前,小于 5 的最新版本是
v4.1.2
。
BUILD
file BUILD
文件Hover on the TS icon
of the chalk
package in NPM: chalk
.将鼠标悬停在
chalk
中chalk
包的TS icon
上: chalk 。 It will show the following message:它将显示以下消息:
This package contains built-in TypeScript declarations
这个包包含内置的TypeScript 声明
Which means it does not need @type/chalk
package.这意味着它不需要
@type/chalk
包。 And that's why it caused BUILD file not found in directory '@types/chalk'
error.这就是为什么它导致
BUILD file not found in directory '@types/chalk'
错误。
So that, go to BUILD
file and delete this line "@npm//@types/chalk",
under ts_project -> deps -> []因此,转到
BUILD
文件并在 ts_project -> deps -> [] 下删除这一行"@npm//@types/chalk",
BUILD
fileBUILD
文件load("@npm//@bazel/typescript:index.bzl", "ts_project")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
ts_project(
name = "lib",
srcs = glob(["src/*.ts"]),
deps = [
"@npm//@types/node",
"@npm//chalk",
],
)
nodejs_binary(
name = "bin",
data = [":lib"],
entry_point = "src/lib.js",
)
After the above, you script will works fine.完成上述操作后,您的脚本将正常工作。
IMPORTANT: Chalk 5 is ESM.
重要提示:粉笔 5 是 ESM。 If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now.
如果您想将 Chalk 与 TypeScript 或构建工具一起使用,您现在可能想要使用 Chalk 4。 Read more.
阅读更多。
There are a few fixes for this issue.有一些针对此问题的修复程序。
1. Use Chalk 4 (recommended for TypeScript) 1. 使用 Chalk 4(推荐用于 TypeScript)
The first solution is to use Chalk 4. You will not be missing out on many features by downgrading to Chalk 4.第一个解决方案是使用 Chalk 4。通过降级到 Chalk 4,您不会错过许多功能。
To do this, you need to first change your versions in package.json
.为此,您需要首先更改
package.json
中的版本。 Change them to this.将它们更改为此。
{
"dependencies": {
"chalk": "4.1.2"
}
}
Note: TypeScript types ( @types/chalk
) aren't required, as Chalk has built-in TypeScript declarations.注意:TypeScript 类型 (
@types/chalk
) 不是必需的,因为 Chalk 具有内置的 TypeScript 声明。
After that, you can delete the node_modules
folder to remove all of the packages.之后,您可以删除
node_modules
文件夹以删除所有包。
Then, we can run the installation command again to reinstall chalk
, as well as the other packages that you have.然后,我们可以再次运行安装命令来重新安装
chalk
以及您拥有的其他软件包。
$ npm install
Then, as Chalk 4 doesn't use import
, you need to change your import
to use require()
.然后,由于 Chalk 4 不使用
import
,您需要将import
更改为使用require()
。
const chalk = require("chalk");
This should correctly install Chalk 4.这应该正确安装 Chalk 4。
2. Change type
to module
2.将
type
更改为module
The second solution is to change the type
to module
in package.json
.第二种解决方案是将
package.json
中的type
更改为module
。 This will allow ESM imports in your files.这将允许在您的文件中导入 ESM。
{
"type": "module"
}
You do not need to change anything else in your files.您无需更改文件中的任何其他内容。 However, you can't use CommonJS imports (eg
require()
) with this set.但是,您不能在此集合中使用 CommonJS 导入(例如
require()
)。
3. Use the import()
function 3.使用
import()
函数
Another solution is to use the import()
function, without adding the type: module
in the package.json
file.另一种解决方案是使用
import()
函数,而不在package.json
文件中添加type: module
。
To do this, you first need to make sure that this line of code isn't in your package.json
file.为此,您首先需要确保这行代码不在您的
package.json
文件中。
{
"type": "module"
}
Then, you need to change the import
statement for chalk
to the following.然后,您需要将
chalk
的import
语句更改为以下内容。
const chalk = await import("chalk");
This function allows both the CommonJS module setting set, as well as an ESM-like import
statement.此函数允许设置 CommonJS 模块设置,以及类似 ESM 的
import
语句。 You just need to make sure that you are using the LTS version of Node.js, which supports top-level await
.您只需要确保您使用的是支持顶级
await
的 LTS 版本的 Node.js。
In conclusion, these solutions should help solve your issue.总之,这些解决方案应该有助于解决您的问题。 However, the first solution is recommended for TypeScript.
但是,对于 TypeScript,建议使用第一个解决方案。
I faced the same kind of error when package.json
wasn't at the root of the folder.当
package.json
不在文件夹的根目录时,我遇到了同样的错误。
Setting package_path = "/"
in yarn_install
fixed the problem.在
yarn_install
中设置package_path = "/"
解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.