简体   繁体   English

创建一个全局npm包

[英]Creating a global npm package

I found this article that talks about creating global packages: https://bretkikehara.wordpress.com/2013/05/02/nodejs-creating-your-first-global-module/ 我发现这篇文章讨论了如何创建全局包: https : //bretkikehara.wordpress.com/2013/05/02/nodejs-creating-your-first-global-module/

So I've got a bin value declared in my package.json like this: 所以我在package.json声明了一个bin值,如下所示:

"bin": {
    "my-server": "./bin/start-server.js"
  }

And in start-server.js I have a reference to ../server.js which is what I run to start my express server up. start-server.js我引用了../server.js ,这是我启动快速服务器所运行的。 After I do an npm install -g in my project folder and run my-server it works just fine, however when I navigate out of the project folder and run my-server the server seems to start, but none of my stylesheets or client-side javascript includes are showing up. 在我的项目文件夹中执行npm install -g并运行my-server它工作正常,但是当我从项目文件夹中导航并运行my-server ,服务器似乎启动了,但是样式表或客户端-旁边的javascript出现了。

In my server.js file I'm defining these paths like this: 在我的server.js文件中,我定义了以下路径:

app.set('views', path.join(__dirname, '/public'));
app.set('view engine', 'ejs');
app.use(express.static('public'));

Is there a different way I should be setting this up so that no matter where someone calls my-server from it has the correct path to the stylesheets? 我应该以其他方式进行设置,以便无论有人在哪里调用my-server都具有指向样式表的正确路径?

I figured the answer to this out. 我想出了答案。 In my server.js file I had to add __dirname to the express.static() expression. 在我的server.js文件中,我必须将__dirname添加到express.static()表达式中。 So this is what it looks like: 这就是它的样子:

app.use(express.static(__dirname + '/public'));

This makes it work just fine so I can call my-server from anywhere after doing npm install -g 这样就可以正常工作,因此我可以在执行npm install -g之后从任何地方调用my-server

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

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