简体   繁体   English

使用Browserify / Babel / node.js是否可以要求基于可变字符串的文件

[英]Using Browserify/Babel/node.js can you require a file based on a variable string

Accoring to this question you can require a path based on a string at runtime in node.js. 根据这个问题,您可能需要在运行时在node.js中基于字符串的路径。

Strangely this seems to work for me okay in node server side, but doesn't seem to work client side using Babel/Browserify 奇怪的是,这似乎在节点服务器端对我来说很好,但是在使用Babel / Browserify的客户端看来却不起作用

For this particular error, I basically had a lot of mock json data I wanted to require for testing, when the actual API was down. 对于这个特定的错误,当实际的API失效时,我基本上require测试很多模拟json数据。

  // slug comes into the function and could for example be 'movies'
  let modulePath = '../mock/products/' + slug + '.js'
  let data = require(modulePath)

This gives me the following error 这给我以下错误

  Failed! Error: Cannot find module '../mock/products/movies.js'

If I change it to let modulePath = '../mock/products/movies.js' it will be no problem retrieving the data... and as I mention if I run this server side it has no problem, but is no good when I run the code client side. 如果我将其更改为let modulePath = '../mock/products/movies.js' ,则检索数据将没有问题...而且正如我所提到的,如果我在此服务器端运行,则没有问题,但是不好当我运行代码客户端时。

I am using babel/babelify to transpile the ES6 code to ES5 我正在使用babel / babelify将ES6代码转换为ES5

Using the following command to build with browserify 使用以下命令与browserify一起构建

browserify --debug -t [babelify] client.js > public/js/bundle.js

My Dev devDependencies in my package.json are as follows: 我在package.json中的Dev devDependencies如下:

  "devDependencies": {
    "browserify": "^8.0.3",
    "babel": "^4.0.1",
    "babelify": "~6.1.2"
  }

Browserify can only compute the dependencies if they are statically analyzable. Browserify仅在可以静态分析的情况下才能计算依赖关系。 It cannot know which modules to bundle if you are generating the import dynamically. 如果您动态生成导入,则它无法知道要捆绑的模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js / browserify require()不适用于变量,但确实适用于字符串文字 - node.js/browserify require() not working on variable, but does work on string literal 为什么我不能在 node.js (browserify) 的 require() 函数中使用变量作为参数? - Why can I not use a variable as parameter in the require() function of node.js (browserify)? 如何从文件中导出字符串,并在导出的NODE.js文件中将require()导出到另一个文件中? - How can I export a string from a file and require() the exported string on another file in NODE.js? 使用 Browserify 加载 Node.js 模块 - Loading Node.js Module using Browserify 使用 require 在另一个文件中访问 Node.js 中立即调用的函数表达式变量 - Accessing an Immediately Invoked Function Expression variable in Node.js in another file using require 使用带有 node.js 的 require 加载远程 js 文件 - Loading remote js file using require with node.js 无法使用 node.js 文件系统和 browserify 将数据传递到 JSON 文件 - cannot pass data to a JSON file using node.js file-system & browserify node.js和browserify错误 - node.js and browserify error Node.js和Browserify的ReferenceError - ReferenceError with Node.js and Browserify Node.js 需要将路径存储在变量中 - Node.js require path stored in a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM