简体   繁体   English

NOde.js / Express App找不到某些node_modules

[英]NOde.js/Express App can't find some node_modules

I use several Node/Express modules in my app, and everything works fine for every module as long as I do const module = require('module'); 我在我的应用程序中使用了几个Node / Express模块​​,只要我执行const module = require('module');每个模块的所有功能都可以正常工作const module = require('module'); . I don't need to define a static path for these modules as app.use(express.static(path.join(__dirname, 'public'))); 我不需要为这些模块定义静态路径为app.use(express.static(path.join(__dirname, 'public'))); .

However, for the sweetalert module, if I define in my layout.pug (base pug file) script(src="/node_modules/sweetalert/dist/sweetalert.min.js") , I get a 404 Error (not found) unless I include in app.js the following static path: app.use("/node_modules", express.static(__dirname + "/node_modules")); 但是,对于sweetalert模块,如果我在我的layout.pug (基础哈巴狗文件) script(src="/node_modules/sweetalert/dist/sweetalert.min.js") ,除非出现404错误(未找到),否则我在app.js中包括以下静态路径: app.use("/node_modules", express.static(__dirname + "/node_modules")); .

My question is: is this the normal behaviour or is it something I'm not doing right? 我的问题是:这是正常的行为还是我没有做对的事情? (I'm kinda confused why I have to define a static path just for one of several modules I use. (我有点困惑,为什么我必须为我使用的几个模块之一定义静态路径。

Here's whats going on: 这是怎么回事:

app.use(express.static(path.join(__dirname, 'public'))); is declaring that the public directory is accessible to the browser. 声明浏览器可以访问公用目录。 You should put all your front end resources in that folder. 您应该将所有前端资源放在该文件夹中。 This will help separate what can be accessed from the server and what can be accessed from the client. 这将有助于区分可从服务器访问的内容和可从客户端访问的内容。

When you reference script(src="/node_modules/sweetalert/dist/sweetalert.min.js") the browser throws a 404 because that file is not located in the public directory, therefore off limits to the browser. 当您引用script(src="/node_modules/sweetalert/dist/sweetalert.min.js") ,浏览器将抛出404,因为该文件不在公共目录中,因此浏览器无法访问。

Adding this line app.use("/node_modules", express.static(__dirname + "/node_modules")); 添加此行app.use("/node_modules", express.static(__dirname + "/node_modules")); "fixes" your issue but now exposes all your node_modules to the browser. “修复”您的问题,但现在将所有node_modules公开给浏览器。 This probably isn't a good idea and I'm sure a security expert could elaborate why this shouldn't be done. 这可能不是一个好主意,我敢肯定,安全专家可以详细说明为什么不应该这样做。

How I would resolve this issue: Go through your .pug code and look at any resources your front end requires. 我将如何解决此问题:浏览您的.pug代码,查看前端所需的任何资源。 Then copy them over to the public folder and fix your references to use the copy of the resource. 然后将它们复制到公用文件夹,并修复您的引用以使用资源的副本。

Here's an example of a script I use to move resources from the node_module directory to a public/assets directory: 这是我用来将资源从node_module目录移动到public / assets目录的脚本示例:

build.js: build.js:

const path = require('path');
var fs = require('fs');

const ASSETS = [
    'jquery/dist/jquery.min.js',
    'sweetalert/dist/sweetalert.min.js'
];

if (!fs.existsSync('./public/assets')){
    fs.mkdirSync('./public/assets');
}

ASSETS.map(asset => {
    let filename = asset.substring(asset.lastIndexOf("/") + 1);
    let from = path.resolve(__dirname, `./node_modules/${asset}`)
    let to = path.resolve(__dirname, `./public/assets/${filename}`)
    if (fs.existsSync(from)) {
        fs.createReadStream(from).pipe(fs.createWriteStream(to));
     } else {
        console.log(`${from} does not exist.\nUpdate the build.js script with the correct file paths.`)
        process.exit(1)
    }
});

then I update my package.json to include this in the scripts: 然后我将package.json更新为包含在脚本中:

package.json: package.json:

"scripts": {
    "build": "node ./build.js || true",
    "start": "node ./bin/www"
}

then in any of my views pages I reference the resource by using the new path 然后在我的任何视图页面中,我都使用新路径引用资源

random.pug: random.pug:

script(src="/assets/jquery.min.js")
script(src="/assets/sweetalert.min.js")

Finally before you deploy your app you now must run the following command: npm run build then npm start 最后,在部署应用程序之前,现在必须运行以下命令: npm run build然后npm start

You will only need to run the build command if your front end resources change. 仅当前端资源发生更改时,才需要运行build命令。 So if you only ever use sweetalert.min.js you will only need to run the build the first time you run your app. 因此,如果您仅使用sweetalert.min.js ,则仅在首次运行应用程序时才需要运行构建。 If later on you add another resource aNewResource.js you will need to update the build.js file and run npm run build again. 如果以后再添加另一个资源aNewResource.js ,则需要更新build.js文件并再次运行npm run build

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM