0

我需要在我的 javascript 代码中使用类似功能的 Node.js 功能require(),所以我正在使用 watchify,但它仍然给我错误Uncaught ReferenceError: require is not defined at jsfile.js:3,尽管 watchify 捆绑代码并检查到bundle.js文件中,如果我在 localhost 上进行测试,一切都会容易得多,但我已经在 heroku 中托管了我的网站,但我不知道命令队列,我应该先执行 git 命令(git add . git commit -m "commit" git push heroku master)然后 watchify(npm run watch)还是先 watchify,然后再执行 git 命令?还是其他地方有问题?这是我的 package.json 代码(部分)

"scripts": {
  "start": "node ilia.js",
  "watch": "npx watchify ./public/jsfile.js -o ./public/bundle.js"
},

"devDependencies": {
  "watchify": "^3.11.1"
}

并在我的 html 中链接 javascript 文件(bundle.js、jsfile.js)

<script src="jsfile.js"></script>
<script src="bundle.js"></script>

所以我该怎么做?

4

1 回答 1

1

Watchify 会创建一个捆绑文件,其中包含require()处理了 s 的所有代码。

require()在您的原始代码中不起作用;您应该只在 HTML 页面中包含捆绑包,而不是原始 JS 文件。

于 2019-05-10T15:50:27.780 回答