I'm developing a Node application with several modules.
My node-application is transpiled with Babel to /dist/app
.
This is an example-structure
.
|- main
| |- config.js
| |- factories
| | |- example.js
This is config.js:
const ex = require("/main/factories/example");
I launch config.js with node dist/app/main/config.js
.
The resulting error is:
Error: Cannot find module '/main/factories/example";
However when using const ex = require("./factories/example");
it works as it should.
This problem only occurs on Windows (testing Windows 8.1), both OS X and Linux are fine.
What is the problem here?
I'm developing a Node application with several modules.
My node-application is transpiled with Babel to /dist/app
.
This is an example-structure
.
|- main
| |- config.js
| |- factories
| | |- example.js
This is config.js:
const ex = require("/main/factories/example");
I launch config.js with node dist/app/main/config.js
.
The resulting error is:
Error: Cannot find module '/main/factories/example";
However when using const ex = require("./factories/example");
it works as it should.
This problem only occurs on Windows (testing Windows 8.1), both OS X and Linux are fine.
What is the problem here?
Share Improve this question edited Jan 28, 2016 at 12:44 Hedge asked Jan 28, 2016 at 11:31 HedgeHedge 16.8k45 gold badges154 silver badges261 bronze badges 3-
Could you flesh out the code sample a bit? What we can see so far doesn't actually call
require
. – N3dst4 Commented Jan 28, 2016 at 12:08 -
I only forgot the
require
– Hedge Commented Jan 28, 2016 at 12:44 - It's the other way around, the code works as expected on Windows. – Shanoor Commented Jan 28, 2016 at 14:02
2 Answers
Reset to default 4It's the other way around, the code works as expected on Windows. /main/factories/example
means C:/main/factories/example
on Windows. It works on OSX/Linux because of some reason (NODE_PATH being set probably). I'd suggest to not rely on a side effect to have a working code and don't use relative path either (entirely dependant on the working directory), you should build your absolute path like this:
const ex = require(__dirname + "/factories/example");
I think maybe the NODE_PATH
cause this your issue. Refer to this article Better local require() paths for Node.js
. There are several ways to require local node modules
The Symlink.
Create a symlink under node_modules to your app directory:
- Linux:
ln -nsf node_modules app
- Windows:
mklink /D app node_modules
- Linux:
The Module
Install some module:
npm install app-module-path --save
In your app.js, before any require() calls:
require('app-module-path').addPath(__dirname + '/app');
In your very/far/away/module.js:
var Article = require('models/article');
The startup script
Linux, create app.sh in your project root:
#!/bin/sh NODE_PATH=. node app.js
Windows, create app.bat in your project root:
@echo off cmd.exe /C "set NODE_PATH=.&& node app.js"
Hope it could help you.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745278546a4620169.html
评论列表(0条)