小编Lau*_*rbe的帖子

Nodejs 如何处理相对路径?

我有一个关于 Nodejs 路径处理的问题。我知道他们在文档中有一个关于此内容的页面,但它不包含我需要的内容。

基本上,我有一个文件,其中包含引用文件的相对路径(在本例中为 png)。

现在,根据我调用文件的位置,图片要么找到,要么找不到(因为文件系统中被调用的点发生变化)。

我使用的是 'sharp' 框架,'sharp('./picture.png')' 与 require 类似。

Example:

File 'render.js' :
const pic = sharp('./picture.png')

Calling:

cmd\examplePath> node render.js //picture is found

cmd> node ./examplePath/render.js //picture is not found

Run Code Online (Sandbox Code Playgroud)
The location of the picture relative to the file stays the same at all times!
Run Code Online (Sandbox Code Playgroud)

我现在的问题是我所描述的内容是否符合 Nodejs 的预期,或者是否有问题。我需要做什么才能从任何地方调用该文件并使其仍然有效?

任何提示表示赞赏。谢谢 :)

javascript node.js sharp

3
推荐指数
1
解决办法
2506
查看次数

标签 统计

javascript ×1

node.js ×1

sharp ×1