Uncaught ReferenceError: require is not defined (How to use a javascript library from the command line, or an html file)
Uncaught ReferenceError: require is not defined (How to use a javascript library from the command line, or an html file)
我是 javascript 和一般编程的新手。我想用这个:
https://github.com/CesiumGS/gltf-pipeline
它是一种将模型转换为压缩格式的工具。
这是我的代码:
const gltfPipeline = require("gltf-pipeline");
const fsExtra = require("fs-extra");
const processGltf = gltfPipeline.processGltf;
const gltf = fsExtra.readJsonSync("model.gltf");
const options = {
dracoOptions: {
compressionLevel: 10,
},
separateTextures: true,
};
processGltf(gltf, options).then(function (results) {
fsExtra.writeJsonSync("modeldraco.gltf", results.gltf);
console.log('done');
const separateResources = results.separateResources;
for (const relativePath in separateResources) {
if (separateResources.hasOwnProperty(relativePath)) {
const resource = separateResources[relativePath];
fsExtra.writeFileSync(relativePath, resource);
}
}
});
我复制了这个文件,将它保存为 compress.js
(因为它押韵)然后我 运行 它与
node compress.js
- 这就是我 运行 一个 python 文件的方式。
错误是:Cannot find module 'gltf-pipeline'
这是有道理的。所以,我做了:
node -r gltf-pipeline compress.js
但我得到同样的错误。
所以,我搬到了 HTML/JS,在那里我制作了一个 index.html 文件并链接了一个 <script>
标签 compress.js
和 gltf-pipeline
index.js
文件。这些是错误:
index.js:3 Uncaught ReferenceError: module is not defined
at index.js:3
(anonymous) @ index.js:3
compress.js:3 Uncaught ReferenceError: require is not defined
那么这是怎么做到的呢?无论是作为网页还是命令行都会有所帮助。
顺便说一句,这是我的文件夹结构,也许这就是问题所在。
basefolder|
|- gltf-pipeline| library files in here
|- compress.js
|- index.html
|- model.gltf
gltf-pipeline 在用作命令行工具时有效。
没有
你不能用 Javascript 做到这一点。
JS 服务于客户端。
当 NodeJS 在服务器上运行时。
这些是节点和浏览器JS之间的differences。
也为模块错误尝试
npm install gltf-pipeline
查看NPM安装节点包
另请查看 NodeJS Tutorial
我是 javascript 和一般编程的新手。我想用这个: https://github.com/CesiumGS/gltf-pipeline 它是一种将模型转换为压缩格式的工具。 这是我的代码:
const gltfPipeline = require("gltf-pipeline");
const fsExtra = require("fs-extra");
const processGltf = gltfPipeline.processGltf;
const gltf = fsExtra.readJsonSync("model.gltf");
const options = {
dracoOptions: {
compressionLevel: 10,
},
separateTextures: true,
};
processGltf(gltf, options).then(function (results) {
fsExtra.writeJsonSync("modeldraco.gltf", results.gltf);
console.log('done');
const separateResources = results.separateResources;
for (const relativePath in separateResources) {
if (separateResources.hasOwnProperty(relativePath)) {
const resource = separateResources[relativePath];
fsExtra.writeFileSync(relativePath, resource);
}
}
});
我复制了这个文件,将它保存为 compress.js
(因为它押韵)然后我 运行 它与
node compress.js
- 这就是我 运行 一个 python 文件的方式。
错误是:Cannot find module 'gltf-pipeline'
这是有道理的。所以,我做了:
node -r gltf-pipeline compress.js
但我得到同样的错误。
所以,我搬到了 HTML/JS,在那里我制作了一个 index.html 文件并链接了一个 <script>
标签 compress.js
和 gltf-pipeline
index.js
文件。这些是错误:
index.js:3 Uncaught ReferenceError: module is not defined
at index.js:3
(anonymous) @ index.js:3
compress.js:3 Uncaught ReferenceError: require is not defined
那么这是怎么做到的呢?无论是作为网页还是命令行都会有所帮助。 顺便说一句,这是我的文件夹结构,也许这就是问题所在。
basefolder|
|- gltf-pipeline| library files in here
|- compress.js
|- index.html
|- model.gltf
gltf-pipeline 在用作命令行工具时有效。
没有
你不能用 Javascript 做到这一点。
JS 服务于客户端。
当 NodeJS 在服务器上运行时。
这些是节点和浏览器JS之间的differences。
也为模块错误尝试
npm install gltf-pipeline
查看NPM安装节点包 另请查看 NodeJS Tutorial