[英]Uncaught ReferenceError: require is not defined (How to use a javascript library from the command line, or an html file)
I'm new to javascript and programming in general.我是 javascript 和编程的新手。 I would like to use this: https://github.com/CesiumGS/gltf-pipeline It's a tool to convert models into compressed formats.
我想用这个: https : //github.com/CesiumGS/gltf-pipeline这是一个将模型转换为压缩格式的工具。 This is my code:
这是我的代码:
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);
}
}
});
I copied this file, saved it as compress.js
(because it rhymes) and I then ran it with node compress.js
- this is how I'd run a python file.我复制了这个文件,将它保存为
compress.js
(因为它押韵),然后我用node compress.js
运行它 - 这就是我运行 python 文件的方式。
Error is: Cannot find module 'gltf-pipeline'
which makes sense.错误是:
Cannot find module 'gltf-pipeline'
。 So, I did:所以我做了:
node -r gltf-pipeline compress.js
but I get the same error. node -r gltf-pipeline compress.js
但我得到同样的错误。
So, I moved to HTML/JS, where I made an index.html file and linked with a <script>
tag compress.js
and the gltf-pipeline
index.js
file.所以,我转向 HTML/JS,在那里我制作了一个 index.html 文件并与
<script>
标签compress.js
和gltf-pipeline
index.js
文件链接。 These are the errors:这些是错误:
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
So how is this done?那么这是如何做到的呢? Either as a webpage or command line would be helpful.
作为网页或命令行都会有所帮助。 This is my folder structure by the way, maybe that's the issue.
顺便说一下,这是我的文件夹结构,也许这就是问题所在。
basefolder|
|- gltf-pipeline| library files in here
|- compress.js
|- index.html
|- model.gltf
gltf-pipeline works when used as a command line tool. gltf-pipeline 在用作命令行工具时工作。
You can't do that with Javascript.你不能用 Javascript 做到这一点。
JS is served to the client. JS 服务于客户端。 Whilst NodeJS runs on a server.
虽然 NodeJS 在服务器上运行。
These are the diffrences between node and browser JS.这些都是diffrences节点和浏览器JS之间。
also for the module error try也为模块错误尝试
npm install gltf-pipeline
Look at NPM to install Node Packages Also take a look at the NodeJS Tutorial看NPM安装 Node Packages 也看一下NodeJS 教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.